﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QSettings>
#include <QTextCodec>
#include <QDir>
#include <QFileInfoList>
#include <QFileInfo>
#include <QFileDialog>
#include <QDateTime>
#include <QThread>
#include <QDesktopServices>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // 初始化界面
//    setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint);
    ui->Main_TabB->tabBar()->hide();
    ui->Main_TabC->tabBar()->hide();

    ui->msg_Box->hide();
    ui->Main_BtMotorA->hide();
    ui->Main_BtPLCManage->hide();
    ui->CCD_continueBt->hide();


    //注册数据类型 不然信号槽会数据丢失
    qRegisterMetaType<CameraData>("CameraData");
    qRegisterMetaType<CsvData>("CsvData");


    // 设置CCD1~CCD4按钮槽函数
    connect(ui->CCD2_OpenFileBt, SIGNAL(clicked()), this, SLOT(on_CCD1_OpenFileBt_clicked()));
    connect(ui->CCD3_OpenFileBt, SIGNAL(clicked()), this, SLOT(on_CCD1_OpenFileBt_clicked()));
    connect(ui->CCD4_OpenFileBt, SIGNAL(clicked()), this, SLOT(on_CCD1_OpenFileBt_clicked()));
    connect(ui->CCD5_OpenFileBt, SIGNAL(clicked()), this, SLOT(on_CCD1_OpenFileBt_clicked()));
    connect(ui->CCD6_OpenFileBt, SIGNAL(clicked()), this, SLOT(on_CCD1_OpenFileBt_clicked()));
    connect(ui->CCD7_OpenFileBt, SIGNAL(clicked()), this, SLOT(on_CCD1_OpenFileBt_clicked()));
    connect(ui->CCD8_OpenFileBt, SIGNAL(clicked()), this, SLOT(on_CCD1_OpenFileBt_clicked()));

    connect(ui->CCD2_UpBt, SIGNAL(clicked()), this, SLOT(on_CCD1_UpBt_clicked()));
    connect(ui->CCD3_UpBt, SIGNAL(clicked()), this, SLOT(on_CCD1_UpBt_clicked()));
    connect(ui->CCD4_UpBt, SIGNAL(clicked()), this, SLOT(on_CCD1_UpBt_clicked()));
    connect(ui->CCD5_UpBt, SIGNAL(clicked()), this, SLOT(on_CCD1_UpBt_clicked()));
    connect(ui->CCD6_UpBt, SIGNAL(clicked()), this, SLOT(on_CCD1_UpBt_clicked()));
    connect(ui->CCD7_UpBt, SIGNAL(clicked()), this, SLOT(on_CCD1_UpBt_clicked()));
    connect(ui->CCD8_UpBt, SIGNAL(clicked()), this, SLOT(on_CCD1_UpBt_clicked()));

    connect(ui->CCD2_DownBt, SIGNAL(clicked()), this, SLOT(on_CCD1_DownBt_clicked()));
    connect(ui->CCD3_DownBt, SIGNAL(clicked()), this, SLOT(on_CCD1_DownBt_clicked()));
    connect(ui->CCD4_DownBt, SIGNAL(clicked()), this, SLOT(on_CCD1_DownBt_clicked()));
    connect(ui->CCD5_DownBt, SIGNAL(clicked()), this, SLOT(on_CCD1_DownBt_clicked()));
    connect(ui->CCD6_DownBt, SIGNAL(clicked()), this, SLOT(on_CCD1_DownBt_clicked()));
    connect(ui->CCD7_DownBt, SIGNAL(clicked()), this, SLOT(on_CCD1_DownBt_clicked()));
    connect(ui->CCD8_DownBt, SIGNAL(clicked()), this, SLOT(on_CCD1_DownBt_clicked()));

    connect(ui->CCD2_StaticBt, SIGNAL(clicked()), this, SLOT(on_CCD1_StaticBt_clicked()));
    connect(ui->CCD3_StaticBt, SIGNAL(clicked()), this, SLOT(on_CCD1_StaticBt_clicked()));
    connect(ui->CCD4_StaticBt, SIGNAL(clicked()), this, SLOT(on_CCD1_StaticBt_clicked()));
    connect(ui->CCD5_StaticBt, SIGNAL(clicked()), this, SLOT(on_CCD1_StaticBt_clicked()));
    connect(ui->CCD6_StaticBt, SIGNAL(clicked()), this, SLOT(on_CCD1_StaticBt_clicked()));
    connect(ui->CCD7_StaticBt, SIGNAL(clicked()), this, SLOT(on_CCD1_StaticBt_clicked()));
    connect(ui->CCD8_StaticBt, SIGNAL(clicked()), this, SLOT(on_CCD1_StaticBt_clicked()));


    /* DLG */
    m_dLog = new LogingDlg(this);
    m_dLog->hide();
    m_dPlc = new PlcDlg(this);
    m_dPlc->hide();

    m_dProduct = new ProductDlg(this);
    m_dProduct->hide();

    m_dDataTab = new DataTabDlg(this);
    m_dDataTab->hide();
    m_dDataTab->move(20, 800);

    /* CsvThread */
    m_tCsv = new CsvThread();

    /* THREAD */
    m_tCamera1 = new CameraThread();
    m_tCamera2 = new CameraThread();
    m_tCamera3 = new CameraThread();
    m_tCamera4 = new CameraThread();
    m_tCamera5 = new CameraThread();
    m_tCamera6 = new CameraThread();
    m_tCamera7 = new CameraThread();
    m_tCamera8 = new CameraThread();

    m_tAlg1 = new AlgThread();
    m_tAlg2 = new AlgThread();
    m_tAlg3 = new AlgThread();
    m_tAlg4 = new AlgThread();
    m_tAlg5 = new AlgThread();
    m_tAlg6 = new AlgThread();
    m_tAlg7 = new AlgThread();
    m_tAlg8 = new AlgThread();

    m_tSaveImg1 = new SaveImgThread();
    m_tSaveImg2 = new SaveImgThread();
    m_tSaveImg3 = new SaveImgThread();
    m_tSaveImg4 = new SaveImgThread();
    m_tSaveImg5 = new SaveImgThread();
    m_tSaveImg6 = new SaveImgThread();
    m_tSaveImg7 = new SaveImgThread();
    m_tSaveImg8 = new SaveImgThread();

    // Connect Camera and Alg Thread event
    connect(m_tCamera1->m_worker, &CameraWorker::sigAlgStart, m_tAlg1->m_worker, &AlgWorker::start);
    connect(m_tCamera2->m_worker, &CameraWorker::sigAlgStart, m_tAlg2->m_worker, &AlgWorker::start);
    connect(m_tCamera3->m_worker, &CameraWorker::sigAlgStart, m_tAlg3->m_worker, &AlgWorker::start);
    connect(m_tCamera4->m_worker, &CameraWorker::sigAlgStart, m_tAlg4->m_worker, &AlgWorker::start);
    connect(m_tCamera5->m_worker, &CameraWorker::sigAlgStart, m_tAlg5->m_worker, &AlgWorker::start);
    connect(m_tCamera6->m_worker, &CameraWorker::sigAlgStart, m_tAlg6->m_worker, &AlgWorker::start);
    connect(m_tCamera7->m_worker, &CameraWorker::sigAlgStart, m_tAlg7->m_worker, &AlgWorker::start);
    connect(m_tCamera8->m_worker, &CameraWorker::sigAlgStart, m_tAlg8->m_worker, &AlgWorker::start);


    connect(m_tAlg1->m_worker, &AlgWorker::sigFrameNum, m_tCamera1->m_worker, &CameraWorker::SlotsFrameNums);
    connect(m_tAlg2->m_worker, &AlgWorker::sigFrameNum, m_tCamera2->m_worker, &CameraWorker::SlotsFrameNums);
    connect(m_tAlg3->m_worker, &AlgWorker::sigFrameNum, m_tCamera3->m_worker, &CameraWorker::SlotsFrameNums);
    connect(m_tAlg4->m_worker, &AlgWorker::sigFrameNum, m_tCamera4->m_worker, &CameraWorker::SlotsFrameNums);
    connect(m_tAlg5->m_worker, &AlgWorker::sigFrameNum, m_tCamera5->m_worker, &CameraWorker::SlotsFrameNums);
    connect(m_tAlg6->m_worker, &AlgWorker::sigFrameNum, m_tCamera6->m_worker, &CameraWorker::SlotsFrameNums);
    connect(m_tAlg7->m_worker, &AlgWorker::sigFrameNum, m_tCamera7->m_worker, &CameraWorker::SlotsFrameNums);
    connect(m_tAlg8->m_worker, &AlgWorker::sigFrameNum, m_tCamera8->m_worker, &CameraWorker::SlotsFrameNums);





    connect(m_tAlg1->m_worker, &AlgWorker::sigSaveImgStart, m_tSaveImg1->m_worker, &SaveImgWorker::start);
    connect(m_tAlg2->m_worker, &AlgWorker::sigSaveImgStart, m_tSaveImg2->m_worker, &SaveImgWorker::start);
    connect(m_tAlg3->m_worker, &AlgWorker::sigSaveImgStart, m_tSaveImg3->m_worker, &SaveImgWorker::start);
    connect(m_tAlg4->m_worker, &AlgWorker::sigSaveImgStart, m_tSaveImg4->m_worker, &SaveImgWorker::start);
    connect(m_tAlg5->m_worker, &AlgWorker::sigSaveImgStart, m_tSaveImg5->m_worker, &SaveImgWorker::start);
    connect(m_tAlg6->m_worker, &AlgWorker::sigSaveImgStart, m_tSaveImg6->m_worker, &SaveImgWorker::start);
    connect(m_tAlg7->m_worker, &AlgWorker::sigSaveImgStart, m_tSaveImg7->m_worker, &SaveImgWorker::start);
    connect(m_tAlg8->m_worker, &AlgWorker::sigSaveImgStart, m_tSaveImg8->m_worker, &SaveImgWorker::start);

    connect(m_tAlg8->m_worker, &AlgWorker::sigCameraResult, this, &MainWindow::CameraResult);


    // Connect product event
    connect(m_dProduct, &ProductDlg::sigProToMain, this, &MainWindow::SearchProFiles);
    connect(m_dProduct->m_dProCreat, &ProCreateDlg::sigProCreateToMain, this, &MainWindow::SearchProFiles);

    connect(m_dDataTab, &DataTabDlg::sigAlgSwitch, this, &MainWindow::AlgSwResult);

    Log("初始化相机");
    // 读取Config 配置文件
    // 初始化 分配参数
    RdConfigIni();
    SetAlgHideUi(0);
//    // 检测相机 个数 是否为配置数 没有报错  不运行启动
    m_control.initSome();
    m_control.UpdateCameraList();
    m_nCurrentCamera = m_control.cameras().size();
    // 创建文件  赋值存图地址
    CreateCameraSaveFile();
    if (m_nCurrentCamera < m_nCameraNum){
        switch (m_nCurrentCamera) {
        case 0:
            Log("Cannot find Any camera!", 1);
            break;
        default:
            break;
        }
    }
    else{
        // 初始化 相机线程
        m_tCamera1->Initialize();
       /* m_tCamera2->Initialize();
        m_tCamera3->Initialize();
        m_tCamera4->Initialize();
        m_tCamera5->Initialize();
        m_tCamera6->Initialize();
        m_tCamera7->Initialize();
        m_tCamera8->Initialize();*/
        /// 获取检测参数

    }
    SearchProFiles();
    RdProductIni(file_list[m_nProductNum]);



    // 定时器
    // 定时器
    myTimer = new QTimer(this);
    connect(myTimer, &QTimer::timeout, this, &MainWindow::systimer);
    myTimer->start(1000);

    // 保存 检测参数tab 标签
    m_tabA = ui->tab_Alg->widget(0);
    m_tabB = ui->tab_Alg->widget(1);
    m_tabC = ui->tab_Alg->widget(2);
    m_tabD = ui->tab_Alg->widget(3);
    m_tabE = ui->tab_Alg->widget(4);
    m_tabF = ui->tab_Alg->widget(5);
    m_tabG = ui->tab_Alg->widget(6);
    m_tabH = ui->tab_Alg->widget(7);
    m_tabI = ui->tab_Alg->widget(8);
    m_tabJ = ui->tab_Alg->widget(9);
}

MainWindow::~MainWindow()
{
    SaveToIni();
    m_control.deleteAll();
    delete ui;
}
void MainWindow::systimer(){
    if (m_bStart){
        stopTime = 0;
        // 计算速率
        m_nTime++;
        if (m_nTime - m_nOldTime == 3){
            m_nOldTime = m_nTime;
            strRev = QString("%1%2").arg(QString::number((((float)m_nCameraResult - (float)m_nRev) / 3) * 60))
                    .arg(" pcs/min");
            m_nRev = m_nCameraResult;
            ui->Main_Blabe_2->setText(strRev);
        }


        m_nSaveTime++;
        if (m_nSaveTime == 300){
            m_nSaveTime = 0;
            SaveToIni();
        }
        // 记录运行时长
        SetRunTime();
    }
    else{
        if (stopTime < 30)
        {
            stopTime++;
        }

        if (stopTime == 20){
            SetStopParam();

            //创建  Csv 数据
            if (m_nSaveData){
                m_csData.csStopTime = GetTime();

                m_nMainCurrent = 1;
                updateAlgInfo();
                ShowAlgData(0);
                m_tCsv->WriteIn(m_csData);
            }

        }
    }
    //    // 判断PLC连接
    IsPlcConnect();
    // 连续硬触发
    if (m_nMainCurrent > 0){
        if (m_nContinuous){
            on_CCD_singleBt_clicked();
            //on_CCD1_StaticBt_clicked();
        }
    }

    //    // 设置时钟
    //    SetClock();

}

void MainWindow::SetRunTime(){
    m_sRunTime = QString("%1h:%2m:%3s").arg(m_nHH).arg(m_nMM, 2, 10, QLatin1Char('0')).arg(m_nSS, 2, 10, QLatin1Char('0'));
    ui->Main_Blabe_7->setText(m_sRunTime);
    m_nSS++;
    if (m_nSS > 59){
        m_nSS = 0;
        m_nMM++;
    }
    if (m_nMM > 59){
        m_nMM = 0;
        m_nHH++;
    }
}

void MainWindow::IsPlcConnect(){
    if (m_dPlc->m_tPlc->m_worker->m_bPlcConnect != bConnect){
        bConnect = m_dPlc->m_tPlc->m_worker->m_bPlcConnect;
        if (bConnect){
            Log("成功：PLC连接成功");
        }
        else{
            Log("错误：PLC连接失败", 1);
        }
    }
    else{
        return;
    }
}



void MainWindow::on_Main_BtMin_clicked()
{
    // min
    setWindowState(Qt::WindowMinimized);
}

void MainWindow::on_Main_BtQuite_clicked()
{
    // quit
    if (!(QMessageBox::question(this, tr("Exit"), QString::fromLocal8Bit("退出？"), tr("Yes"), tr("No"))))
    {
        QApplication* app;
        app->exit(0);
    }
}

void MainWindow::on_Main_BtStartAndStop_clicked()
{
    if (m_nCurrentCamera < 4){
        return;
    }
    OpenHalconWindows();
    // 启动与停止 采集
    if (m_nMainStart){
        m_dPlc->Stop();
        m_bStart = false;
        m_dPlc->m_tPlc->m_worker->m_bReceive = false;
        ui->Main_BtStartAndStop->setStyleSheet("border-image: url(:/main/img/main/Main_BtStart.png)");
    }
    else{
        m_dPlc->Start();
        m_nPlcIndex = 0;
        m_bStart = true;
        SetStartParam();
        m_dPlc->m_tPlc->m_worker->m_bReceive = true;
        m_csData.csStarTime = GetTime();
        m_csData.csStarNum = QString::number(m_nCameraResult);
        ui->Main_BtStartAndStop->setStyleSheet("border-image: url(:/main/img/main/Main_BtStop.png)");

        SaveToIni();
    }
    m_nMainStart = 1 - m_nMainStart;
}


void MainWindow::OpenHalconWindows()
{
    //重置数据

    m_tAlg1->m_worker->m_nResult = 0;
    m_tAlg2->m_worker->m_nResult = 0;
    m_tAlg3->m_worker->m_nResult = 0;
    m_tAlg4->m_worker->m_nResult = 0;
    m_tAlg5->m_worker->m_nResult = 0;
    m_tAlg6->m_worker->m_nResult = 0;
    m_tAlg7->m_worker->m_nResult = 0;
    m_tAlg8->m_worker->m_nResult = 0;


    Hlong hWind;
    // Halcon
    if (m_nOpenHalcon){
        return;
    }
    m_nOpenHalcon = 1;
    hWind = ui->Main_CCD1->winId();
    HalconCpp::OpenWindow(0, 0, ui->Main_CCD1->width(), ui->Main_CCD1->height(), hWind, "", "", &m_hvMainHandle1);
    hWind = ui->Main_CCD2->winId();
    HalconCpp::OpenWindow(0, 0, ui->Main_CCD2->width(), ui->Main_CCD2->height(), hWind, "", "", &m_hvMainHandle2);
    hWind = ui->Main_CCD3->winId();
    HalconCpp::OpenWindow(0, 0, ui->Main_CCD3->width(), ui->Main_CCD3->height(), hWind, "", "", &m_hvMainHandle3);
    hWind = ui->Main_CCD4->winId();
    HalconCpp::OpenWindow(0, 0, ui->Main_CCD4->width(), ui->Main_CCD4->height(), hWind, "", "", &m_hvMainHandle4);
    hWind = ui->Main_CCD5->winId();
    HalconCpp::OpenWindow(0, 0, ui->Main_CCD5->width(), ui->Main_CCD5->height(), hWind, "", "", &m_hvMainHandle5);
    hWind = ui->Main_CCD6->winId();
    HalconCpp::OpenWindow(0, 0, ui->Main_CCD6->width(), ui->Main_CCD6->height(), hWind, "", "", &m_hvMainHandle6);
    hWind = ui->Main_CCD7->winId();
    HalconCpp::OpenWindow(0, 0, ui->Main_CCD7->width(), ui->Main_CCD7->height(), hWind, "", "", &m_hvMainHandle7);
    hWind = ui->Main_CCD8->winId();
    HalconCpp::OpenWindow(0, 0, ui->Main_CCD8->width(), ui->Main_CCD8->height(), hWind, "", "", &m_hvMainHandle8);


    hWind = ui->CCD1_Wind->winId();
    HalconCpp::OpenWindow(0, 0, ui->CCD1_Wind->width(), ui->CCD1_Wind->height(), hWind, "", "", &c1_hvCCDHandle);
    hWind = ui->CCD2_Wind->winId();
    HalconCpp::OpenWindow(0, 0, ui->CCD2_Wind->width(), ui->CCD2_Wind->height(), hWind, "", "", &c2_hvCCDHandle);
    hWind = ui->CCD3_Wind->winId();
    HalconCpp::OpenWindow(0, 0, ui->CCD3_Wind->width(), ui->CCD3_Wind->height(), hWind, "", "", &c3_hvCCDHandle);
    hWind = ui->CCD4_Wind->winId();
    HalconCpp::OpenWindow(0, 0, ui->CCD4_Wind->width(), ui->CCD4_Wind->height(), hWind, "", "", &c4_hvCCDHandle);
    hWind = ui->CCD5_Wind->winId();
    HalconCpp::OpenWindow(0, 0, ui->CCD5_Wind->width(), ui->CCD5_Wind->height(), hWind, "", "", &c5_hvCCDHandle);
    hWind = ui->CCD6_Wind->winId();
    HalconCpp::OpenWindow(0, 0, ui->CCD6_Wind->width(), ui->CCD6_Wind->height(), hWind, "", "", &c6_hvCCDHandle);
    hWind = ui->CCD7_Wind->winId();
    HalconCpp::OpenWindow(0, 0, ui->CCD7_Wind->width(), ui->CCD7_Wind->height(), hWind, "", "", &c7_hvCCDHandle);
    hWind = ui->CCD8_Wind->winId();
    HalconCpp::OpenWindow(0, 0, ui->CCD8_Wind->width(), ui->CCD8_Wind->height(), hWind, "", "", &c8_hvCCDHandle);


    m_tCamera1->m_worker->m_cData.m_nIndex = 1;
    m_tCamera2->m_worker->m_cData.m_nIndex = 2;
    m_tCamera3->m_worker->m_cData.m_nIndex = 3;
    m_tCamera4->m_worker->m_cData.m_nIndex = 4;
    m_tCamera5->m_worker->m_cData.m_nIndex = 5;
    m_tCamera6->m_worker->m_cData.m_nIndex = 6;
    m_tCamera7->m_worker->m_cData.m_nIndex = 7;
    m_tCamera8->m_worker->m_cData.m_nIndex = 8;


    m_tCamera1->m_worker->m_cData.hv_WindowHandle = m_hvMainHandle1;
    m_tCamera2->m_worker->m_cData.hv_WindowHandle = m_hvMainHandle2;
    m_tCamera3->m_worker->m_cData.hv_WindowHandle = m_hvMainHandle3;
    m_tCamera4->m_worker->m_cData.hv_WindowHandle = m_hvMainHandle4;
    m_tCamera5->m_worker->m_cData.hv_WindowHandle = m_hvMainHandle5;
    m_tCamera6->m_worker->m_cData.hv_WindowHandle = m_hvMainHandle6;
    m_tCamera7->m_worker->m_cData.hv_WindowHandle = m_hvMainHandle7;
    m_tCamera8->m_worker->m_cData.hv_WindowHandle = m_hvMainHandle8;



    m_tCamera1->start();
   /* m_tCamera2->start();
    m_tCamera3->start();
    m_tCamera4->start();
    m_tCamera5->start();
    m_tCamera6->start();
    m_tCamera7->start();
    m_tCamera8->start();*/

    m_bSet = true;
}

void MainWindow::RdConfigIni()
{
    Log("读取Config配置文件");
    QString iniFilePath = "Config/config.ini";
    QSettings configIniRead(iniFilePath, QSettings::IniFormat);
    configIniRead.setIniCodec(QTextCodec::codecForName("UTF_8"));

    // 获取相机
    m_nCameraNum = configIniRead.value("CameraNum/Num").toInt();
    for (int i = 0; i < m_nCameraNum; i++){
        QString camera_fileName =
                QString("%1%2%3")
                .arg("Camera_").arg(i + 1).arg("/rStrCameraID");
        m_cameras.push_back(configIniRead.value(camera_fileName).toString());

        camera_fileName = QString("set CCD%1 SN:%2")
                .arg(i + 1).arg(configIniRead.value(camera_fileName).toString());
        Log(camera_fileName.toStdString());
    }



	m_tCamera1->m_worker->m_cData.qStrCameraID = m_cameras[0];
	/*m_tCamera2->m_worker->m_cData.qStrCameraID = m_cameras[1];
	m_tCamera3->m_worker->m_cData.qStrCameraID = m_cameras[2];
	m_tCamera4->m_worker->m_cData.qStrCameraID = m_cameras[3];
	m_tCamera5->m_worker->m_cData.qStrCameraID = m_cameras[4];
	m_tCamera6->m_worker->m_cData.qStrCameraID = m_cameras[5];
	m_tCamera7->m_worker->m_cData.qStrCameraID = m_cameras[6];
	m_tCamera8->m_worker->m_cData.qStrCameraID = m_cameras[7];*/

    RdManufactureIni();
}



void MainWindow::changeEvent(QEvent *event)
{
    if (event->type() == QEvent::WindowStateChange)
    {
        if (!isMinimized())
        {
            setAttribute(Qt::WA_Mapped);
        }
    }          QWidget::changeEvent(event);
}

void MainWindow::SetAlgHideUi(int index)
{
    QLineEdit* lE;
    QLabel* lB;
    if (index){

        for (int i = 0; i < 18; i++){
            lB = this->findChild<QLabel*>("tex_" + QString::number(i + 1));
            lB->show();
        }
        // 正方形全检
        ui->ccd_rect_1->show();
        ui->ccd_rect_2->show();
        ui->ccd_rect_9->show();
        ui->ccd_rect_20->show();
        ui->ccd_rect_24->show();
        ui->ccd_rect_28->show();
        ui->ccd_rect_32->show();
        ui->ccd_rect_36->show();

        ui->ccd_rect_6->show();
        ui->ccd_rect_7->show();
        ui->ccd_rect_8->show();

        ui->ccd_rect_13->show();
        ui->ccd_rect_14->show();
        ui->ccd_rect_15->show();

        ui->ccd_rect_16->show();
        ui->ccd_rect_17->show();
        ui->ccd_rect_18->show();
        ui->ccd_rect_19->show();
    }
    else{
        // 正方形全检
        for (int i = 0; i < 18; i++){
            lB = this->findChild<QLabel*>("tex_" + QString::number(i + 1));
            lB->hide();
        }
        ui->ccd_rect_1->hide();
        ui->ccd_rect_2->hide();
        ui->ccd_rect_9->hide();
        ui->ccd_rect_20->hide();
        ui->ccd_rect_24->hide();
        ui->ccd_rect_28->hide();
        ui->ccd_rect_32->hide();
        ui->ccd_rect_36->hide();

        ui->ccd_rect_6->hide();
        ui->ccd_rect_7->hide();
        ui->ccd_rect_8->hide();

        ui->ccd_rect_13->hide();
        ui->ccd_rect_14->hide();
        ui->ccd_rect_15->hide();

        ui->ccd_rect_16->hide();
        ui->ccd_rect_17->hide();
        ui->ccd_rect_18->hide();
        ui->ccd_rect_19->hide();

        // 方形缺陷

        ui->tex_21->hide();
        ui->tex_22->hide();
        ui->tex_23->hide();
        ui->tex_24->hide();
        ui->tex_25->hide();
        ui->tex_26->hide();
        ui->tex_27->hide();
        ui->tex_28->hide();
        ui->tex_29->hide();
        ui->tex_30->hide();

        ui->ccd_defect_21->hide();
        ui->ccd_defect_22->hide();
        ui->ccd_defect_23->hide();
        ui->ccd_defect_24->hide();
        ui->ccd_defect_25->hide();
        ui->ccd_defect_26->hide();
        ui->ccd_defect_27->hide();
        ui->ccd_defect_28->hide();
        ui->ccd_defect_29->hide();
        ui->ccd_defect_30->hide();


    }
}

void MainWindow::getCameraAttribute()
{
    //if (m_nCurrentCamera < 4){
    //    return;
    //}
    ui->CCD_Edit1->setText(QString::number(m_tCurrentCamera->m_worker->m_control->getExposureTime()));
    ui->CCD_Edit2->setText(QString::number(m_tCurrentCamera->m_worker->m_control->getOffsetX()));
    ui->CCD_Edit3->setText(QString::number(m_tCurrentCamera->m_worker->m_control->getOffsetY()));
    ui->CCD_Edit4->setText(QString::number(m_tCurrentCamera->m_worker->m_control->getCameraHeight()));
    ui->CCD_Edit5->setText(QString::number(m_tCurrentCamera->m_worker->m_control->getCameraWidth()));
    ui->CCD_Edit8->setText(QString::number(m_tCurrentCamera->m_worker->m_control->getGain()));
}


void MainWindow::ShowImage(HalconCpp::HObject img, HalconCpp::HTuple hvHandle)
{
    HalconCpp::HTuple w, h;
    HalconCpp::GetImageSize(img, &w, &h);
    HalconCpp::SetPart(hvHandle, 0, 0, h, w);
    HalconCpp::DispObj(img, hvHandle);
}


void MainWindow::on_Main_TabA_currentChanged(int index)
{
    m_nMainCurrent = index;
    switch (index) {
    case 0:
        // 跳转主页面
        m_tCamera1->m_worker->m_cData.hv_WindowHandle = m_hvMainHandle1;
        m_tCamera2->m_worker->m_cData.hv_WindowHandle = m_hvMainHandle2;
        m_tCamera3->m_worker->m_cData.hv_WindowHandle = m_hvMainHandle3;
        m_tCamera4->m_worker->m_cData.hv_WindowHandle = m_hvMainHandle4;
        m_tCamera5->m_worker->m_cData.hv_WindowHandle = m_hvMainHandle5;
        m_tCamera6->m_worker->m_cData.hv_WindowHandle = m_hvMainHandle6;
        m_tCamera7->m_worker->m_cData.hv_WindowHandle = m_hvMainHandle7;
        m_tCamera8->m_worker->m_cData.hv_WindowHandle = m_hvMainHandle8;
        break;
    case 1:
        // 跳转CCD1
        m_tCurrentCamera = m_tCamera1;
        m_tCurrentAlg = m_tAlg1;
        m_tCamera1->m_worker->m_cData.hv_WindowHandle = c1_hvCCDHandle;
        break;
    case 2:
        // 跳转CCD2
        m_tCurrentCamera = m_tCamera2;
        m_tCurrentAlg = m_tAlg2;
        m_tCamera2->m_worker->m_cData.hv_WindowHandle = c2_hvCCDHandle;
        //
        break;
    case 3:
        m_tCurrentCamera = m_tCamera3;
        m_tCurrentAlg = m_tAlg3;
        m_tCamera3->m_worker->m_cData.hv_WindowHandle = c3_hvCCDHandle;
        // 跳转CCD3
        break;
    case 4:
        m_tCurrentCamera = m_tCamera4;
        m_tCurrentAlg = m_tAlg4;
        m_tCamera4->m_worker->m_cData.hv_WindowHandle = c4_hvCCDHandle;
        // 跳转CCD4
        break;
    case 5:
        m_tCurrentCamera = m_tCamera5;
        m_tCurrentAlg = m_tAlg5;
        m_tCamera5->m_worker->m_cData.hv_WindowHandle = c5_hvCCDHandle;
        break;
    case 6:
        m_tCurrentCamera = m_tCamera6;
        m_tCurrentAlg = m_tAlg6;
        m_tCamera6->m_worker->m_cData.hv_WindowHandle = c6_hvCCDHandle;
        break;
    case 7:
        m_tCurrentCamera = m_tCamera7;
        m_tCurrentAlg = m_tAlg7;
        m_tCamera7->m_worker->m_cData.hv_WindowHandle = c7_hvCCDHandle;
        // 跳转CCD4
        break;
    case 8:
        m_tCurrentCamera = m_tCamera8;
        m_tCurrentAlg = m_tAlg8;
        m_tCamera8->m_worker->m_cData.hv_WindowHandle = c8_hvCCDHandle;
        // 跳转CCD4
        break;
    default:
        return;
        break;
    }
    if (index){
//        m_dDataTab->show();
    }
    else{
        m_dDataTab->hide();
    }

    if (index > 0){
        getCameraAttribute();
        ui->Main_TabC->setCurrentIndex(1);
        ui->Main_TabB->setCurrentIndex(1);
        ui->msg_Box->show();
        updateAlgInfo();
    }
    else{
        ui->Main_TabC->setCurrentIndex(0);
        ui->Main_TabB->setCurrentIndex(0);
        ui->msg_Box->hide();
    }

    ShowAlgData(0);
}


void MainWindow::OpenFileImg(int index)
{
    QString OpenFile;
    //打开文件夹中的图片文件
    OpenFile = QFileDialog::getOpenFileName(this,
                                            "Please choose an image file",
                                            "",
                                            "Image Files(*.jpg *.png *.bmp *.pgm *.pbm);;All(*.*)");
    if (OpenFile != "")
    {

        OpenHalconWindows();

        // 获取当前文件所有图片名称
        QFileInfo fi;
        QString file_name;
        QString file_path;
        QStringList file_list;
        int file_index = 0;
        fi = QFileInfo(OpenFile);
        file_name = fi.fileName();
        file_path = fi.absolutePath();

        QDir dir(file_path);
        file_list = dir.entryList(QDir::Files | QDir::NoDotAndDotDot);
        file_index = file_list.indexOf(file_name);

        m_vFilePath[index] = file_path;
        m_vFileList[index] = file_list;
        m_vFileIndex[index] = file_index;

        CarouselImage(index);

    }
}

void MainWindow::CarouselImage(int index)
{
    QString OpenFile = QString("%1/%2").arg(m_vFilePath[index]).arg(m_vFileList[index][m_vFileIndex[index]]);

    QTextCodec *code = QTextCodec::codecForName("GB2312");//解决中文路径问题
    std::string name = code->fromUnicode(OpenFile).data();
    isOpenImg = true;

    HalconCpp::ReadImage(&m_hImage, name.c_str());
    QPushButton * pushBt;

    switch (index) {
    case 0:
        pushBt = ui->CCD1_Path;
        m_hvHandle = c1_hvCCDHandle;
        break;
    case 1:
        pushBt = ui->CCD2_Path;
        m_hvHandle = c2_hvCCDHandle;
        break;
    case 2:
        pushBt = ui->CCD3_Path;
        m_hvHandle = c3_hvCCDHandle;
        break;
    case 3:
        pushBt = ui->CCD4_Path;
        m_hvHandle = c4_hvCCDHandle;
        break;
    case 4:
        pushBt = ui->CCD5_Path;
        m_hvHandle = c5_hvCCDHandle;
        break;
    case 5:
        pushBt = ui->CCD6_Path;
        m_hvHandle = c6_hvCCDHandle;
        break;
    case 6:
        pushBt = ui->CCD7_Path;
        m_hvHandle = c7_hvCCDHandle;
        break;
    case 7:
        pushBt = ui->CCD8_Path;
        m_hvHandle = c8_hvCCDHandle;
        break;
    default:
        break;
    }
    pushBt->setText(OpenFile);
    ShowImage(m_hImage, m_hvHandle);
}

void MainWindow::on_CCD1_OpenFileBt_clicked()
{
    OpenFileImg(m_nMainCurrent - 1);
}


void MainWindow::on_CCD1_UpBt_clicked()
{
    if (!isOpenImg){
        return;
    }
    ImgUp(m_nMainCurrent - 1);
}

void MainWindow::on_CCD1_DownBt_clicked()
{
    if (!isOpenImg){
        return;
    }
    ImgDown(m_nMainCurrent - 1);
}
void MainWindow::ImgUp(int index)
{
    if (m_vFilePath[index] == NULL){
        return;
    }
    m_vFileIndex[index]--;
    if (m_vFileIndex[index] == -1){
        m_vFileIndex[index] = m_vFileList[index].size() - 1;
    }
    CarouselImage(index);
}

void MainWindow::ImgDown(int index)
{
    if (m_vFilePath[index] == NULL){
        return;
    }
    m_vFileIndex[index]++;
    if (m_vFileIndex[index] == m_vFileList[index].size()){
        m_vFileIndex[index] = 0;
    }
    CarouselImage(index);
}


void MainWindow::SetCombox()
{
    ui->Main_ComboxProduct->clear();
    file_names.clear();
    // comboBox
    for (int i = 0; i < file_list.size(); i++){
        file_names.append(file_list[i].left(file_list[i].indexOf(".ini")));
        ui->Main_ComboxProduct->addItem(file_names[i]);
    }
    m_dProduct->file_names = file_names;
    m_dProduct->updateListInfo();

    if (m_dProduct->m_nIndex){
        ui->Main_ComboxProduct->setCurrentIndex(m_dProduct->m_nIndex);
    }
}



void MainWindow::on_CCD1_StaticBt_clicked()
{
    QTime time;
    if (!isOpenImg){
        return;
    }
    int result = 0;
    QString msg;
    AlgThread* pAlg;
    HalconCpp::HTuple hvCCDHandle;
    switch (m_nMainCurrent) {
    case 1:
        pAlg = m_tAlg1;
        hvCCDHandle = c1_hvCCDHandle;
        break;
    case 2:
        pAlg = m_tAlg2;
        hvCCDHandle = c2_hvCCDHandle;
        break;
    case 3:
        pAlg = m_tAlg3;
        hvCCDHandle = c3_hvCCDHandle;
        break;
    case 4:
        pAlg = m_tAlg4;
        hvCCDHandle = c4_hvCCDHandle;
        break;
    case 5:
        pAlg = m_tAlg5;
        hvCCDHandle = c5_hvCCDHandle;
        break;
    case 6:
        pAlg = m_tAlg6;
        hvCCDHandle = c6_hvCCDHandle;
        break;
    case 7:
        pAlg = m_tAlg7;
        hvCCDHandle = c7_hvCCDHandle;
        break;
    case 8:
        pAlg = m_tAlg8;
        hvCCDHandle = c8_hvCCDHandle;
        break;
    default:
        break;
    }
    time.start();
    pAlg->m_worker->CCDNum = m_nMainCurrent;
    result = pAlg->m_worker->CCD_Alg(m_hImage);
    pAlg->m_worker->show(pAlg->m_worker->OutImg, hvCCDHandle, result);
    msg = QString("Time:%1s").arg(time.elapsed() / 1000.0);

    ui->msg_time->setText(msg);
    Msg_Box(result);
    ShowAlgData(0);
}


void MainWindow::Msg_Box(int nMsg)
{
    QString strMsg1;
    QString strMsg2;
    QString NG = "color: rgb(255, 0, 0);";
    QString OK = "color: rgb(85, 255, 255);";
    QString Abnormal = "color: rgb(255, 255, 0);";
    QString Confuse = "color: rgb(85, 255, 0);";
    QString msgColor1;
    QString msgColor2;

    switch (nMsg / 10) {
    case 1:
        strMsg2 = QString::fromLocal8Bit("缺陷:混料");
        msgColor2 = Confuse;
        break;
    case 2:
        strMsg2 = QString::fromLocal8Bit("缺陷:通过");
        msgColor2 = OK;
        break;
    case 3:
        strMsg2 = QString::fromLocal8Bit("缺陷:错误");
        msgColor2 = NG;
        break;
    case 4:
        strMsg2 = QString::fromLocal8Bit("缺陷:异常");
        msgColor2 = Abnormal;
        break;
    }


    switch (nMsg % 10) {
    case 1:
        strMsg1 = QString::fromLocal8Bit("尺寸:混料");
        msgColor1 = Confuse;
        break;
    case 2:
        strMsg1 = QString::fromLocal8Bit("尺寸:通过");
        msgColor1 = OK;
        break;
    case 3:
        strMsg1 = QString::fromLocal8Bit("尺寸:错误");
        msgColor1 = NG;
        break;
    case 4:
        strMsg1 = QString::fromLocal8Bit("尺寸:异常");
        msgColor1 = Abnormal;
        break;
    }

    ui->msg_size->setStyleSheet(msgColor1);
    ui->msg_defect->setStyleSheet(msgColor2);

    ui->msg_size->setText(strMsg1);
    ui->msg_defect->setText(strMsg2);
}



void MainWindow::on_CCD_changeBT_clicked()
{
    // 修改相机 基本属性
    setCameraAttribute();
}
void MainWindow::setCameraAttribute()
{
    m_tCurrentCamera->m_worker->m_control->setExposureTime(ui->CCD_Edit1->text().toInt());
    if (!m_bSet)
    {
        m_tCurrentCamera->m_worker->m_control->setOffsetX(ui->CCD_Edit2->text().toInt());
        m_tCurrentCamera->m_worker->m_control->setOffsetY(ui->CCD_Edit3->text().toInt());
        m_tCurrentCamera->m_worker->m_control->setCameraHeight(ui->CCD_Edit4->text().toInt());
        m_tCurrentCamera->m_worker->m_control->setCameraWidth(ui->CCD_Edit5->text().toInt());
    }

    m_tCurrentCamera->m_worker->m_control->setCameraGain(ui->CCD_Edit8->text().toInt());
    getCameraAttribute();

    QMessageBox::information(this, QString::fromLocal8Bit("成功"), QString::fromLocal8Bit("修改成功！"));
}

QString MainWindow::GetTime(){
    QDateTime current_date_time = QDateTime::currentDateTime();
    QString current_date = current_date_time.toString("yyyy-MM-dd hh:mm:ss");
    return current_date;
}
void MainWindow::Log(std::string strMsg, int type)
{
    QListWidgetItem *item = new QListWidgetItem();
    QString Msg = QString::fromLocal8Bit(strMsg.c_str());
    Msg = QString("%1:%2").arg(GetTime()).arg(Msg);
    QFont font;
    font.setBold(true);
    font.setFamily("微软雅黑");
    font.setPointSize(12);
    item->setText(Msg);
    item->setFont(font);

    switch (type) {
    case 0:
        item->setTextColor(QColor(0, 0, 0));
        break;
    case 1:
        item->setTextColor(QColor(255, 0, 127));
        break;
    default:
        break;
    }
    ui->Main_msgList->insertItem(0, item);
}

void MainWindow::on_Main_BtPic_clicked()
{
    // 切换 原图 瑕疵图


    m_swSaveImg.nSwPic = 1 - m_swSaveImg.nSwPic;
    if (m_swSaveImg.nSwPic){
        ui->Main_BtPic->setText(QString::fromLocal8Bit("瑕疵图"));
    }
    else{
        ui->Main_BtPic->setText(QString::fromLocal8Bit("原图"));
    }
}

void MainWindow::on_Main_BtNGorAND_clicked()
{
    // 切换 NG OK
    m_swSaveImg.nSwNg++;
    if (m_swSaveImg.nSwNg == 3){
        m_swSaveImg.nSwNg = 0;
    }
    switch (m_swSaveImg.nSwNg){
    case 0:
        ui->Main_BtNGorAND->setText(QString::fromLocal8Bit("异常"));
        break;
    case 1:
        ui->Main_BtNGorAND->setText("NG");
        break;
    case 2:
        ui->Main_BtNGorAND->setText("All");
        break;
    }
    m_tSaveImg1->m_worker->m_nSaveType = m_swSaveImg.nSwNg;
    m_tSaveImg2->m_worker->m_nSaveType = m_swSaveImg.nSwNg;
    m_tSaveImg3->m_worker->m_nSaveType = m_swSaveImg.nSwNg;
    m_tSaveImg4->m_worker->m_nSaveType = m_swSaveImg.nSwNg;
    m_tSaveImg5->m_worker->m_nSaveType = m_swSaveImg.nSwNg;
    m_tSaveImg6->m_worker->m_nSaveType = m_swSaveImg.nSwNg;
    m_tSaveImg7->m_worker->m_nSaveType = m_swSaveImg.nSwNg;
    m_tSaveImg8->m_worker->m_nSaveType = m_swSaveImg.nSwNg;


}

void MainWindow::on_Main_SBtCCD1_clicked()
{
    m_swSaveImg.nCCD1 = 1 - m_swSaveImg.nCCD1;
    QPushButton* pSwBt = ui->Main_SBtCCD1;
    ShowSwitchBt(pSwBt, m_swSaveImg.nCCD1, 0);
}

void MainWindow::on_Main_SBtCCD2_clicked()
{
    m_swSaveImg.nCCD2 = 1 - m_swSaveImg.nCCD2;
    QPushButton* pSwBt = ui->Main_SBtCCD2;
    ShowSwitchBt(pSwBt, m_swSaveImg.nCCD2, 0);
}

void MainWindow::on_Main_SBtCCD3_clicked()
{
    m_swSaveImg.nCCD3 = 1 - m_swSaveImg.nCCD3;
    QPushButton* pSwBt = ui->Main_SBtCCD3;
    ShowSwitchBt(pSwBt, m_swSaveImg.nCCD3, 0);
}

void MainWindow::on_Main_SBtCCD4_clicked()
{
    m_swSaveImg.nCCD4 = 1 - m_swSaveImg.nCCD4;
    QPushButton* pSwBt = ui->Main_SBtCCD4;
    ShowSwitchBt(pSwBt, m_swSaveImg.nCCD4, 0);
}

void MainWindow::on_Main_SBtCCD5_clicked()
{
    m_swSaveImg.nCCD5 = 1 - m_swSaveImg.nCCD5;
    QPushButton* pSwBt = ui->Main_SBtCCD5;
    ShowSwitchBt(pSwBt, m_swSaveImg.nCCD5, 0);
}

void MainWindow::on_Main_SBtCCD6_clicked()
{
    m_swSaveImg.nCCD6 = 1 - m_swSaveImg.nCCD6;
    QPushButton* pSwBt = ui->Main_SBtCCD6;
    ShowSwitchBt(pSwBt, m_swSaveImg.nCCD6, 0);
}

void MainWindow::on_Main_SBtCCD7_clicked()
{
    m_swSaveImg.nCCD7 = 1 - m_swSaveImg.nCCD7;
    QPushButton* pSwBt = ui->Main_SBtCCD7;
    ShowSwitchBt(pSwBt, m_swSaveImg.nCCD7, 0);
}

void MainWindow::on_Main_SBtCCD8_clicked()
{
    m_swSaveImg.nCCD8 = 1 - m_swSaveImg.nCCD8;
    QPushButton* pSwBt = ui->Main_SBtCCD8;
    ShowSwitchBt(pSwBt, m_swSaveImg.nCCD8, 0);
}

void MainWindow::on_Main_CBtCCD1_clicked()
{
    m_swCheck.nCCD1 = 1 - m_swCheck.nCCD1;
    QPushButton* pSwBt = ui->Main_CBtCCD1;
    ShowSwitchBt(pSwBt, m_swCheck.nCCD1, 1);
}
void MainWindow::on_Main_CBtCCD2_clicked()
{
    m_swCheck.nCCD2 = 1 - m_swCheck.nCCD2;
    QPushButton* pSwBt = ui->Main_CBtCCD2;
    ShowSwitchBt(pSwBt, m_swCheck.nCCD2, 1);
}
void MainWindow::on_Main_CBtCCD3_clicked()
{
    m_swCheck.nCCD3 = 1 - m_swCheck.nCCD3;
    QPushButton* pSwBt = ui->Main_CBtCCD3;
    ShowSwitchBt(pSwBt, m_swCheck.nCCD3, 1);
}
void MainWindow::on_Main_CBtCCD4_clicked()
{
    m_swCheck.nCCD4 = 1 - m_swCheck.nCCD4;
    QPushButton* pSwBt = ui->Main_CBtCCD4;
    ShowSwitchBt(pSwBt, m_swCheck.nCCD4, 1);
}
void MainWindow::on_Main_CBtCCD5_clicked()
{
    m_swCheck.nCCD5 = 1 - m_swCheck.nCCD5;
    QPushButton* pSwBt = ui->Main_CBtCCD5;
    ShowSwitchBt(pSwBt, m_swCheck.nCCD5, 1);
}

void MainWindow::on_Main_CBtCCD6_clicked()
{
    m_swCheck.nCCD6 = 1 - m_swCheck.nCCD6;
    QPushButton* pSwBt = ui->Main_CBtCCD6;
    ShowSwitchBt(pSwBt, m_swCheck.nCCD6, 1);
}

void MainWindow::on_Main_CBtCCD7_clicked()
{
    m_swCheck.nCCD7 = 1 - m_swCheck.nCCD7;
    QPushButton* pSwBt = ui->Main_CBtCCD7;
    ShowSwitchBt(pSwBt, m_swCheck.nCCD7, 1);
}

void MainWindow::on_Main_CBtCCD8_clicked()
{
    m_swCheck.nCCD8 = 1 - m_swCheck.nCCD8;
    QPushButton* pSwBt = ui->Main_CBtCCD8;
    ShowSwitchBt(pSwBt, m_swCheck.nCCD8, 1);
}


void MainWindow::ShowSwitchBt(QPushButton *pSwBt, int nState, int nIndex)
{
    // 切换  存图   测量 状态
    m_tAlg1->m_worker->m_swSaveImg = m_swSaveImg;
    m_tAlg2->m_worker->m_swSaveImg = m_swSaveImg;
    m_tAlg3->m_worker->m_swSaveImg = m_swSaveImg;
    m_tAlg4->m_worker->m_swSaveImg = m_swSaveImg;
    m_tAlg5->m_worker->m_swSaveImg = m_swSaveImg;
    m_tAlg6->m_worker->m_swSaveImg = m_swSaveImg;
    m_tAlg7->m_worker->m_swSaveImg = m_swSaveImg;
    m_tAlg8->m_worker->m_swSaveImg = m_swSaveImg;
    QString strSheetA = "border-image: url(:/main/img/main/Main_SBtCCD.png);";
    QString strSheetB = "border-image: url(:/main/img/main/Main_SBtCCD2.png);";
    if (nState){
        pSwBt->setStyleSheet(strSheetA);
    }
    else{
        pSwBt->setStyleSheet(strSheetB);
    }
}

void MainWindow::on_Main_BtPLCManage_clicked()
{
    // 打开PLC 设置
    m_dPlc->show();
}

void MainWindow::CreateCameraSaveFile()
{
    QDir dir;
    QString imgPath = QDir::currentPath() + "/Pic";
    QString CalPath = QDir::currentPath() + "/Cal";
    bool img = dir.exists(imgPath);
    bool cal = dir.exists(CalPath);
    if (!img)
    {
        dir.mkdir(imgPath);

        dir.mkdir(imgPath + "/ccd1");
        dir.mkdir(imgPath + "/ccd1/OK");
        dir.mkdir(imgPath + "/ccd1/NG");
        dir.mkdir(imgPath + "/ccd1/Confuse");
        dir.mkdir(imgPath + "/ccd1/Abnormal");

        dir.mkdir(imgPath + "/ccd2");
        dir.mkdir(imgPath + "/ccd2/OK");
        dir.mkdir(imgPath + "/ccd2/NG");
        dir.mkdir(imgPath + "/ccd2/Confuse");
        dir.mkdir(imgPath + "/ccd2/Abnormal");

        dir.mkdir(imgPath + "/ccd3");
        dir.mkdir(imgPath + "/ccd3/OK");
        dir.mkdir(imgPath + "/ccd3/NG");
        dir.mkdir(imgPath + "/ccd3/Confuse");
        dir.mkdir(imgPath + "/ccd3/Abnormal");

        dir.mkdir(imgPath + "/ccd4");
        dir.mkdir(imgPath + "/ccd4/OK");
        dir.mkdir(imgPath + "/ccd4/NG");
        dir.mkdir(imgPath + "/ccd4/Confuse");
        dir.mkdir(imgPath + "/ccd4/Abnormal");

        dir.mkdir(imgPath + "/ccd5");
        dir.mkdir(imgPath + "/ccd5/OK");
        dir.mkdir(imgPath + "/ccd5/NG");
        dir.mkdir(imgPath + "/ccd5/Confuse");
        dir.mkdir(imgPath + "/ccd5/Abnormal");

        dir.mkdir(imgPath + "/ccd6");
        dir.mkdir(imgPath + "/ccd6/OK");
        dir.mkdir(imgPath + "/ccd6/NG");
        dir.mkdir(imgPath + "/ccd6/Confuse");
        dir.mkdir(imgPath + "/ccd6/Abnormal");

        dir.mkdir(imgPath + "/ccd7");
        dir.mkdir(imgPath + "/ccd7/OK");
        dir.mkdir(imgPath + "/ccd7/NG");
        dir.mkdir(imgPath + "/ccd7/Confuse");
        dir.mkdir(imgPath + "/ccd7/Abnormal");

        dir.mkdir(imgPath + "/ccd8");
        dir.mkdir(imgPath + "/ccd8/OK");
        dir.mkdir(imgPath + "/ccd8/NG");
        dir.mkdir(imgPath + "/ccd8/Confuse");
        dir.mkdir(imgPath + "/ccd8/Abnormal");




    }
    if (!cal)
    {
        dir.mkdir(CalPath);
        dir.mkdir(CalPath + "/Cal_Cricle");

        dir.mkdir(CalPath + "/Cal_Cricle/Pic");
        dir.mkdir(CalPath + "/Cal_Length");
        dir.mkdir(CalPath + "/Cal_Length/Pic");
        QFile filename1(CalPath + "/Cal_Cricle/Cricle.txt");
        filename1.open(QIODevice::WriteOnly);
        filename1.close();
        QFile filename2(CalPath + "/Cal_Length/Length.txt");
        filename2.open(QIODevice::WriteOnly);
        filename2.close();
    }
}

void MainWindow::CameraResult(int m_nResult)
{
    // 发送给PLC 结果
    AlgRule(m_nResult);
    // 界面显示
    ShowUI();
}
void MainWindow::ShowUI(){
    float goodRatio1 = 0;
    float goodRatio2 = 0;
    float goodRatio3 = 0;
    float goodRatio4 = 0;
    float goodRatio5 = 0;
    float goodRatio6 = 0;
    float goodRatio7 = 0;
    float goodRatio8 = 0;

    if (m_nGod1)
    {
        goodRatio1 = float(m_nGod1) / float(m_nCameraResult);
    }
    if (m_nGod2)
    {
        goodRatio2 = float(m_nGod2) / float(m_nCameraResult);
    }
    if (m_nGod3)
    {
        goodRatio3 = float(m_nGod3) / float(m_nCameraResult);
    }
    if (m_nGod4)
    {
        goodRatio4 = float(m_nGod4) / float(m_nCameraResult);
    }
    if (m_nGod5)
    {
        goodRatio5 = float(m_nGod5) / float(m_nCameraResult);
    }
    if (m_nGod6)
    {
        goodRatio6 = float(m_nGod6) / float(m_nCameraResult);
    }

    if (m_nGod7)
    {
        goodRatio7 = float(m_nGod7) / float(m_nCameraResult);
    }
    if (m_nGod8)
    {
        goodRatio8 = float(m_nGod8) / float(m_nCameraResult);
    }

    QString strGoodR1 = QString("%1%2").arg(QString::number(goodRatio1 * 100, 'f', 2)).arg("%");
    QString strGoodR2 = QString("%1%2").arg(QString::number(goodRatio2 * 100, 'f', 2)).arg("%");
    QString strGoodR3 = QString("%1%2").arg(QString::number(goodRatio3 * 100, 'f', 2)).arg("%");
    QString strGoodR4 = QString("%1%2").arg(QString::number(goodRatio4 * 100, 'f', 2)).arg("%");
    QString strGoodR5 = QString("%1%2").arg(QString::number(goodRatio5 * 100, 'f', 2)).arg("%");
    QString strGoodR6 = QString("%1%2").arg(QString::number(goodRatio6 * 100, 'f', 2)).arg("%");
    QString strGoodR7 = QString("%1%2").arg(QString::number(goodRatio7 * 100, 'f', 2)).arg("%");
    QString strGoodR8 = QString("%1%2").arg(QString::number(goodRatio8 * 100, 'f', 2)).arg("%");



    ui->Main_Alabe_7->setText(QString::number(m_nGod1));
    ui->Main_Alabe_8->setText(QString::number(m_nGod2));
    ui->Main_Alabe_9->setText(QString::number(m_nGod3));
    ui->Main_Alabe_10->setText(QString::number(m_nGod4));
    ui->Main_Alabe_11->setText(QString::number(m_nGod5));
    ui->Main_Alabe_12->setText(QString::number(m_nGod6));
    ui->Main_Alabe_27->setText(QString::number(m_nGod7));
    ui->Main_Alabe_28->setText(QString::number(m_nGod8));

    if (m_swCheck.nCCD1){
        ui->Main_Alabe_1->setText(QString::number(m_nCameraResult));
        ui->Main_Alabe_13->setText(strGoodR1);
    }
    if (m_swCheck.nCCD2){
        ui->Main_Alabe_2->setText(QString::number(m_nCameraResult));
        ui->Main_Alabe_14->setText(strGoodR2);
    }
    if (m_swCheck.nCCD3){
        ui->Main_Alabe_3->setText(QString::number(m_nCameraResult));
        ui->Main_Alabe_15->setText(strGoodR3);
    }
    if (m_swCheck.nCCD4){
        ui->Main_Alabe_4->setText(QString::number(m_nCameraResult));
        ui->Main_Alabe_16->setText(strGoodR4);
    }

    if (m_swCheck.nCCD5){
        ui->Main_Alabe_5->setText(QString::number(m_nCameraResult));
        ui->Main_Alabe_17->setText(strGoodR5);
    }
    if (m_swCheck.nCCD6){
        ui->Main_Alabe_6->setText(QString::number(m_nCameraResult));
        ui->Main_Alabe_18->setText(strGoodR6);
    }
    if (m_swCheck.nCCD7){
        ui->Main_Alabe_25->setText(QString::number(m_nCameraResult));
        ui->Main_Alabe_29->setText(strGoodR7);
    }
    if (m_swCheck.nCCD8){
        ui->Main_Alabe_26->setText(QString::number(m_nCameraResult));
        ui->Main_Alabe_30->setText(strGoodR8);
    }




    ui->Main_Alabe_19->setText(QString::number(m_nBad1));
    ui->Main_Alabe_20->setText(QString::number(m_nBad2));
    ui->Main_Alabe_21->setText(QString::number(m_nBad3));
    ui->Main_Alabe_22->setText(QString::number(m_nBad4));
    ui->Main_Alabe_23->setText(QString::number(m_nBad5));
    ui->Main_Alabe_24->setText(QString::number(m_nBad6));
    ui->Main_Alabe_31->setText(QString::number(m_nBad7));
    ui->Main_Alabe_32->setText(QString::number(m_nBad8));

    ui->Main_Blabe_1->setText(QString::number(m_nCameraResult));
    ui->Main_Blabe_3->setText(QString::number(m_nOK));

    float goodRatio = 0;
    float badRatio = 0;
    if (m_nCameraResult)
    {
        goodRatio = float(m_nOK) / float(m_nCameraResult);
        badRatio = float(m_nCameraResult - m_nOK) / float(m_nCameraResult);
    }

    QString strgood = QString("%1%2").arg(QString::number(goodRatio * 100, 'f', 2)).arg("%");
    QString strbad = QString("%1%2").arg(QString::number(badRatio * 100, 'f', 2)).arg("%");
    ui->Main_Blabe_4->setText(strgood);
    ui->Main_Blabe_5->setText(QString::number(m_nCameraResult - m_nOK));
    ui->Main_Blabe_6->setText(strbad);

    ui->Main_Blabe_8->setText(QString::number(m_nNG1));
    ui->Main_Blabe_9->setText(QString::number(m_nNG2));
    ui->Main_Blabe_11->setText(QString::number(m_nNG4));
    ui->Main_Blabe_12->setText(QString::number(m_nOK));

    ShowAlgData(0);

    //检测开始时间,检测结束时间,产品类型,产品批次,产品名,产品规格,初始数量,结束数量,良品数,良品率,不良数,不良率,尺寸不合格数,缺陷不合格数,混料,异常

    m_csData.csGodNum = QString::number(m_nOK);
    m_csData.csBadNum = QString::number(m_nCameraResult - m_nOK);
    m_csData.csGodRatio = strgood;
    m_csData.csBadRatio = strbad;
    m_csData.csNG1 = QString::number(m_nNG1);
    m_csData.csNG2 = QString::number(m_nNG2);
    m_csData.csNG3 = QString::number(m_nNG3);
    m_csData.csNG4 = QString::number(m_nNG4);
    m_csData.csStopNum = QString::number(m_nCameraResult);
}
void MainWindow::AlgRule(int nIndex){
    // OK   105
    // NG1  100   尺寸不良+{1.缺陷不良 2.缺陷OK}
    // NG2  101   缺陷不良+尺寸OK
    // NG3  102   异常（换混料  异常不处理）
    int nNorOK = 101;
    int nNGorOK1 = m_tAlg1->m_worker->m_nNGorOK[nIndex];
    int nNGorOK2 = m_tAlg2->m_worker->m_nNGorOK[nIndex];
    int nNGorOK3 = m_tAlg3->m_worker->m_nNGorOK[nIndex];
    int nNGorOK4 = m_tAlg4->m_worker->m_nNGorOK[nIndex];
    int nNGorOK5 = m_tAlg5->m_worker->m_nNGorOK[nIndex];
    int nNGorOK6 = m_tAlg6->m_worker->m_nNGorOK[nIndex];
    int nNGorOK7 = m_tAlg7->m_worker->m_nNGorOK[nIndex];
    int nNGorOK8 = m_tAlg8->m_worker->m_nNGorOK[nIndex];


    /*qDebug() << "Alg Reuslt: " << m_nCameraResult;
    qDebug()
        << "nNGorOK1: " << nNGorOK1
        << "nNGorOK2: " << nNGorOK2
        << "nNGorOK3: " << nNGorOK3
        << "nNGorOK4: " << nNGorOK4
        << "nNGorOK5: " << nNGorOK5
        << "nNGorOK6: " << nNGorOK6
        << "nNGorOK7: " << nNGorOK7
        << "nNGorOK8: " << nNGorOK8;*/

    QList<int> m_lists = QList<int>() << nNGorOK1 << nNGorOK2 << nNGorOK3 << nNGorOK4
                                      << nNGorOK5 << nNGorOK6 << nNGorOK7 << nNGorOK8;
    m_nCameraResult++;


    if (m_swCheck.nCCD1){
        if (nNGorOK1 == 22){
            m_nGod1++;
        }
        else{
            m_nBad1++;
        }
    }
    else{
        nNGorOK1 = 22;
    }

    if (m_swCheck.nCCD2){
        if (nNGorOK2 == 22){
            m_nGod2++;
        }
        else{
            m_nBad2++;
        }
    }
    else{
        nNGorOK2 = 22;
    }

    if (m_swCheck.nCCD3){
        if (nNGorOK3 == 22){
            m_nGod3++;
        }
        else{
            m_nBad3++;
        }
    }
    else{
        nNGorOK3 = 22;
    }

    if (m_swCheck.nCCD4){
        if (nNGorOK4 == 22){
            m_nGod4++;
        }
        else{
            m_nBad4++;
        }
    }
    else{
        nNGorOK4 = 22;
    }


    if (m_swCheck.nCCD5){
        if (nNGorOK5 == 22){
            m_nGod5++;
        }
        else{
            m_nBad5++;
        }
    }
    else{
        nNGorOK5 = 22;
    }

    if (m_swCheck.nCCD6){
        if (nNGorOK6 == 22){
            m_nGod6++;
        }
        else{
            m_nBad6++;
        }
    }
    else{
        nNGorOK6 = 22;
    }

    if (m_swCheck.nCCD7){
        if (nNGorOK7 == 22){
            m_nGod7++;
        }
        else{
            m_nBad7++;
        }
    }
    else{
        nNGorOK7 = 22;
    }
    if (m_swCheck.nCCD8){
        if (nNGorOK8 == 22){
            m_nGod8++;
        }
        else{
            m_nBad8++;
        }
    }
    else{
        nNGorOK8 = 22;
    }


    if (nNGorOK1 == 22 && nNGorOK2 == 22 && nNGorOK3 == 22 && nNGorOK4 == 22 && nNGorOK5 == 22 && nNGorOK6 == 22 && nNGorOK7 == 22 && nNGorOK8 == 22){
        nNorOK = 105;
        m_nOK++;
        SendPlcResult(nNorOK);
        return;
    }
    else{
        //        for (int i = 0; i < 6; i++){
        //            if (m_lists[i]%10 == 1){
        //                // 混料
        //                nNorOK = 200;
        //                m_nNG4++;
        //                SendPlcResult(nNorOK);
        //                return;
        //            }
        //        }
        for (int i = 0; i < 8; i++){
            if ( m_lists[i] % 10 == 1 || m_lists[i] % 10 == 4 || m_lists[i] / 10 == 4 ){
                //异常  次之 (检测出来 没有动作 分到漏检)
                nNorOK = 200;
                m_nNG4++;
                SendPlcResult(nNorOK);
                return;
            }
        }

        for (int i = 0; i < 8; i++){
            if (m_lists[i] % 10 == 3){
                // 尺寸
                nNorOK = 100;
                m_nNG1++;
                SendPlcResult(nNorOK);
                return;
            }
        }


        // 缺陷
        nNorOK = 101;
        m_nNG2++;
        SendPlcResult(nNorOK);
        return;
    }
}


void MainWindow::SendPlcResult(int result)
{
    m_dPlc->m_tPlc->m_worker->writePlcInfo(RESULT_NUMBER, m_nPlcIndex);
    m_dPlc->m_tPlc->m_worker->writePlcInfo(CAMERA_RESULT, result);
    //qDebug() << "SendPlcResult(105[ok] 101[NG2 QX] 100[NG1 CC] 200[YC]): " << result;
    m_nPlcIndex += 2;
    if (m_nPlcIndex == 1000){
        m_nPlcIndex = 0;
    }
}

void MainWindow::InitializeAlgData()
{

    // 清空 UI界面
    QLabel* m_qLabel;
    QGroupBox* pLabelTab;
    for (int j = 1; j < 8; j++){
        for (int i = 0; i < 11; i++){
            m_qLabel = this->findChild<QLabel*>(QString("CD1_TabA1_%1_%2").arg(j).arg(i));
            m_qLabel->setText("");
        }
        pLabelTab = this->findChild<QGroupBox*>(QString("ccd_Atab_%1").arg(QString::number(j)));
        pLabelTab->setStyleSheet("background-color:transparent;");
        pLabelTab = this->findChild<QGroupBox*>(QString("ccd_Btab_%1").arg(QString::number(j)));
        pLabelTab->setStyleSheet("background-color:transparent;");
    }

    QCheckBox* m_qCBox;
    for (int j = 1; j < 3; j++){
        for (int i = 5; i < 11; i++){
            m_qLabel = this->findChild<QLabel*>(QString("CD1_TabA2_%1_%2").arg(j).arg(i));
            m_qLabel->setText("");
        }
        m_qCBox = this->findChild<QCheckBox*>(QString("CD1_TabA2_%1_0").arg(j));
        m_qCBox->show();
    }


    switch (m_nDefctType) {
    case 1:
        // 缺陷：方形
        m_dDataTab->ui->CD1_TabA2_1_5->setText(ui->ccd_defect_3->text());
        m_dDataTab->ui->CD1_TabA2_2_5->setText(ui->ccd_defect_7->text());
        break;
    case 2:
        // 缺陷：扇形
        m_dDataTab->ui->CD1_TabA2_1_5->setText(ui->ccd_DSector_5->text());
        m_dDataTab->ui->CD1_TabA2_2_5->setText(ui->ccd_DSector_8->text());
        break;
    default:
        for (int j = 1; j < 3; j++){
            m_qCBox = this->findChild<QCheckBox*>(QString("CD1_TabA2_%1_0").arg(j));
            m_qCBox->hide();
        }
        break;
    }


    //  OutPara   CCD1/CCD4 正面 1.宽度  2.平行度 角度 3.长度  4. 平行度   5.角度1  6.角度2  7.角度3 8.角度4
    // Outpara  CCD3  侧面  1.厚度 2.平行度
    // outpara  梯形  0 高度   1平行度  2 左斜边角度   3  右斜边角度
    switch (m_nRectType) {
    case 1: // 正/长方形 全检
        m_dDataTab->ui->CD1_TabA1_1_0->setText(QString::fromLocal8Bit("宽度"));
        m_dDataTab->ui->CD1_TabA1_2_0->setText(QString::fromLocal8Bit("长度"));
        m_dDataTab->ui->CD1_TabA1_3_0->setText(QString::fromLocal8Bit("角度1"));
        m_dDataTab->ui->CD1_TabA1_4_0->setText(QString::fromLocal8Bit("角度2"));
        m_dDataTab->ui->CD1_TabA1_5_0->setText(QString::fromLocal8Bit("角度3"));
        m_dDataTab->ui->CD1_TabA1_6_0->setText(QString::fromLocal8Bit("角度4"));

        // 基本尺寸
        m_dDataTab->ui->CD1_TabA1_1_2->setText(ui->ccd_rect_3->text());
        m_dDataTab->ui->CD1_TabA1_2_2->setText(ui->ccd_rect_10->text());
        m_dDataTab->ui->CD1_TabA1_3_2->setText(ui->ccd_rect_21->text());
        m_dDataTab->ui->CD1_TabA1_4_2->setText(ui->ccd_rect_25->text());
        m_dDataTab->ui->CD1_TabA1_5_2->setText(ui->ccd_rect_29->text());
        m_dDataTab->ui->CD1_TabA1_6_2->setText(ui->ccd_rect_33->text());


        // 上工差

        m_dDataTab->ui->CD1_TabA1_1_3->setText(ui->ccd_rect_4->text());
        m_dDataTab->ui->CD1_TabA1_2_3->setText(ui->ccd_rect_11->text());
        m_dDataTab->ui->CD1_TabA1_3_3->setText(ui->ccd_rect_22->text());
        m_dDataTab->ui->CD1_TabA1_4_3->setText(ui->ccd_rect_26->text());
        m_dDataTab->ui->CD1_TabA1_5_3->setText(ui->ccd_rect_30->text());
        m_dDataTab->ui->CD1_TabA1_6_3->setText(ui->ccd_rect_34->text());


        // 下工差
        m_dDataTab->ui->CD1_TabA1_1_4->setText(ui->ccd_rect_5->text());
        m_dDataTab->ui->CD1_TabA1_2_4->setText(ui->ccd_rect_12->text());
        m_dDataTab->ui->CD1_TabA1_3_4->setText(ui->ccd_rect_23->text());
        m_dDataTab->ui->CD1_TabA1_4_4->setText(ui->ccd_rect_27->text());
        m_dDataTab->ui->CD1_TabA1_5_4->setText(ui->ccd_rect_31->text());
        m_dDataTab->ui->CD1_TabA1_6_4->setText(ui->ccd_rect_35->text());


        // 补偿值
        m_dDataTab->ui->CD1_TabA1_1_6->setText(ui->ccd_rect_2->text());
        m_dDataTab->ui->CD1_TabA1_2_6->setText(ui->ccd_rect_9->text());
        m_dDataTab->ui->CD1_TabA1_3_6->setText(ui->ccd_rect_20->text());
        m_dDataTab->ui->CD1_TabA1_4_6->setText(ui->ccd_rect_24->text());
        m_dDataTab->ui->CD1_TabA1_5_6->setText(ui->ccd_rect_28->text());
        m_dDataTab->ui->CD1_TabA1_6_6->setText(ui->ccd_rect_32->text());

        break;
    case 2: // 梯形
        m_dDataTab->ui->CD1_TabA1_1_0->setText(QString::fromLocal8Bit("高度"));
        m_dDataTab->ui->CD1_TabA1_2_0->setText(QString::fromLocal8Bit("左斜边角度"));
        m_dDataTab->ui->CD1_TabA1_3_0->setText(QString::fromLocal8Bit("右斜边角度"));


        // 基本尺寸
        m_dDataTab->ui->CD1_TabA1_1_2->setText(ui->ccd_trapezoid_3->text());
        m_dDataTab->ui->CD1_TabA1_2_2->setText(ui->ccd_trapezoid_11->text());
        m_dDataTab->ui->CD1_TabA1_3_2->setText(ui->ccd_trapezoid_12->text());



        // 上工差

        m_dDataTab->ui->CD1_TabA1_1_3->setText(ui->ccd_trapezoid_5->text());
        m_dDataTab->ui->CD1_TabA1_2_3->setText(ui->ccd_trapezoid_15->text());
        m_dDataTab->ui->CD1_TabA1_3_3->setText(ui->ccd_trapezoid_16->text());



        // 下工差
        m_dDataTab->ui->CD1_TabA1_1_4->setText(ui->ccd_trapezoid_4->text());
        m_dDataTab->ui->CD1_TabA1_2_4->setText(ui->ccd_trapezoid_13->text());
        m_dDataTab->ui->CD1_TabA1_3_4->setText(ui->ccd_trapezoid_14->text());



        // 补偿值
        m_dDataTab->ui->CD1_TabA1_1_6->setText(ui->ccd_trapezoid_2->text());
        m_dDataTab->ui->CD1_TabA1_2_6->setText(ui->ccd_trapezoid_9->text());
        m_dDataTab->ui->CD1_TabA1_3_6->setText(ui->ccd_trapezoid_10->text());


        break;
    case 3:
        m_dDataTab->ui->CD1_TabA1_1_0->setText(QString::fromLocal8Bit("厚度"));
        // 基本尺寸
        m_dDataTab->ui->CD1_TabA1_1_2->setText(ui->ccd_side_3->text());



        // 上工差
        m_dDataTab->ui->CD1_TabA1_1_3->setText(ui->ccd_side_4->text());

        // 下工差
        m_dDataTab->ui->CD1_TabA1_1_4->setText(ui->ccd_side_5->text());

        // 补偿值
        m_dDataTab->ui->CD1_TabA1_1_6->setText(ui->ccd_side_2->text());

        break;
    case 4:
        m_dDataTab->ui->CD1_TabA1_1_0->setText(QString::fromLocal8Bit("高度"));
        m_dDataTab->ui->CD1_TabA1_2_0->setText(QString::fromLocal8Bit("宽度"));
        // 基本尺寸
        m_dDataTab->ui->CD1_TabA1_1_2->setText(ui->ccd_rectTS_4->text());
        m_dDataTab->ui->CD1_TabA1_2_2->setText(ui->ccd_rectTS_8->text());

        // 上工差
        m_dDataTab->ui->CD1_TabA1_1_3->setText(ui->ccd_rectTS_5->text());
        m_dDataTab->ui->CD1_TabA1_2_3->setText(ui->ccd_rectTS_9->text());

        // 下工差
        m_dDataTab->ui->CD1_TabA1_1_4->setText(ui->ccd_rectTS_6->text());
        m_dDataTab->ui->CD1_TabA1_2_4->setText(ui->ccd_rectTS_10->text());
        // 补偿值
        m_dDataTab->ui->CD1_TabA1_1_6->setText(ui->ccd_rectTS_3->text());
        m_dDataTab->ui->CD1_TabA1_2_6->setText(ui->ccd_rectTS_7->text());

        break;
    case 5:
        m_dDataTab->ui->CD1_TabA1_1_0->setText(QString::fromLocal8Bit("厚度"));
        // 基本尺寸
        m_dDataTab->ui->CD1_TabA1_1_2->setText(ui->ccd_rectTSS_3->text());
        // 上工差
        m_dDataTab->ui->CD1_TabA1_1_3->setText(ui->ccd_rectTSS_4->text());
        // 下工差
        m_dDataTab->ui->CD1_TabA1_1_4->setText(ui->ccd_rectTSS_5->text());
        // 补偿值
        m_dDataTab->ui->CD1_TabA1_1_6->setText(ui->ccd_rectTSS_2->text());


        break;
    case 7:
        m_dDataTab->ui->CD1_TabA1_1_0->setText(QString::fromLocal8Bit("弧边1"));
        m_dDataTab->ui->CD1_TabA1_2_0->setText(QString::fromLocal8Bit("弧边2"));
        m_dDataTab->ui->CD1_TabA1_3_0->setText(QString::fromLocal8Bit("直边3"));
        m_dDataTab->ui->CD1_TabA1_4_0->setText(QString::fromLocal8Bit("直边4"));
        m_dDataTab->ui->CD1_TabA1_5_0->setText(QString::fromLocal8Bit("中直边"));
        m_dDataTab->ui->CD1_TabA1_6_0->setText(QString::fromLocal8Bit("轮廓度"));


        // 基本尺寸
        m_dDataTab->ui->CD1_TabA1_1_2->setText(ui->ccd_sector_3->text());
        m_dDataTab->ui->CD1_TabA1_2_2->setText(ui->ccd_sector_7->text());
        m_dDataTab->ui->CD1_TabA1_3_2->setText(ui->ccd_sector_11->text());
        m_dDataTab->ui->CD1_TabA1_4_2->setText(ui->ccd_sector_15->text());
        m_dDataTab->ui->CD1_TabA1_5_2->setText(ui->ccd_sector_19->text());
        m_dDataTab->ui->CD1_TabA1_6_2->setText("0");


        // 上工差
        m_dDataTab->ui->CD1_TabA1_1_3->setText(ui->ccd_sector_4->text());
        m_dDataTab->ui->CD1_TabA1_2_3->setText(ui->ccd_sector_8->text());
        m_dDataTab->ui->CD1_TabA1_3_3->setText(ui->ccd_sector_12->text());
        m_dDataTab->ui->CD1_TabA1_4_3->setText(ui->ccd_sector_16->text());
        m_dDataTab->ui->CD1_TabA1_5_3->setText(ui->ccd_sector_20->text());
        m_dDataTab->ui->CD1_TabA1_6_3->setText(ui->ccd_sector_24->text());


        // 下工差
        m_dDataTab->ui->CD1_TabA1_1_4->setText(ui->ccd_sector_5->text());
        m_dDataTab->ui->CD1_TabA1_2_4->setText(ui->ccd_sector_9->text());
        m_dDataTab->ui->CD1_TabA1_3_4->setText(ui->ccd_sector_13->text());
        m_dDataTab->ui->CD1_TabA1_4_4->setText(ui->ccd_sector_17->text());
        m_dDataTab->ui->CD1_TabA1_5_4->setText(ui->ccd_sector_21->text());
        m_dDataTab->ui->CD1_TabA1_6_4->setText("0");


        // 补偿值
        m_dDataTab->ui->CD1_TabA1_1_6->setText(ui->ccd_sector_2->text());
        m_dDataTab->ui->CD1_TabA1_2_6->setText(ui->ccd_sector_6->text());
        m_dDataTab->ui->CD1_TabA1_3_6->setText(ui->ccd_sector_10->text());
        m_dDataTab->ui->CD1_TabA1_4_6->setText(ui->ccd_sector_14->text());
        m_dDataTab->ui->CD1_TabA1_5_6->setText(ui->ccd_sector_18->text());
        m_dDataTab->ui->CD1_TabA1_6_6->setText("0");
        break;


    case 8:
        m_dDataTab->ui->CD1_TabA1_1_0->setText(QString::fromLocal8Bit("宽度"));
        m_dDataTab->ui->CD1_TabA1_2_0->setText(QString::fromLocal8Bit("长度"));
        m_dDataTab->ui->CD1_TabA1_3_0->setText(QString::fromLocal8Bit("角度1"));
        m_dDataTab->ui->CD1_TabA1_4_0->setText(QString::fromLocal8Bit("角度2"));

        // 基本尺寸
        m_dDataTab->ui->CD1_TabA1_1_2->setText(ui->ccd_rect_3->text());
        m_dDataTab->ui->CD1_TabA1_2_2->setText(ui->ccd_rect_10->text());
        m_dDataTab->ui->CD1_TabA1_3_2->setText(ui->ccd_rect_21->text());
        m_dDataTab->ui->CD1_TabA1_4_2->setText(ui->ccd_rect_25->text());



        // 上工差

        m_dDataTab->ui->CD1_TabA1_1_3->setText(ui->ccd_rect_4->text());
        m_dDataTab->ui->CD1_TabA1_2_3->setText(ui->ccd_rect_11->text());
        m_dDataTab->ui->CD1_TabA1_3_3->setText(ui->ccd_rect_22->text());
        m_dDataTab->ui->CD1_TabA1_4_3->setText(ui->ccd_rect_26->text());



        // 下工差
        m_dDataTab->ui->CD1_TabA1_1_4->setText(ui->ccd_rect_5->text());
        m_dDataTab->ui->CD1_TabA1_2_4->setText(ui->ccd_rect_12->text());
        m_dDataTab->ui->CD1_TabA1_3_4->setText(ui->ccd_rect_23->text());
        m_dDataTab->ui->CD1_TabA1_4_4->setText(ui->ccd_rect_27->text());



        // 补偿值
        m_dDataTab->ui->CD1_TabA1_1_6->setText(ui->ccd_rect_2->text());
        m_dDataTab->ui->CD1_TabA1_2_6->setText(ui->ccd_rect_9->text());
        m_dDataTab->ui->CD1_TabA1_3_6->setText(ui->ccd_rect_20->text());
        m_dDataTab->ui->CD1_TabA1_4_6->setText(ui->ccd_rect_24->text());
        break;
    default:
        break;
    }
}

void MainWindow::ShowAlgData(int type)
{
    // 检测数据 结构体
    AlgThread* m_tAlg;
    switch (m_nMainCurrent)
    {
    case 1:
        m_tAlg = m_tAlg1;
        break;
    case 2:
        m_tAlg = m_tAlg2;
        break;
    case 3:
        m_tAlg = m_tAlg3;
        break;
    case 4:
        m_tAlg = m_tAlg4;
        break;
    case 5:
        m_tAlg = m_tAlg5;
        break;
    case 6:
        m_tAlg = m_tAlg6;
        break;
    case 7:
        m_tAlg = m_tAlg7;
        break;
    case 8:
        m_tAlg = m_tAlg8;
        break;
    default:
        return;
        break;
    }

    //  OutPara   CCD1/CCD4 正面 1.宽度  2.平行度 3.长度  4. 平行度   5.角度1  6.角度2  7.角度3 8.角度4
    // Outpara  CCD3  侧面  1.厚度 2.平行度
    // outpara  梯形  0 高度   1平行度  2 左斜边角度   3  右斜边角度

    m_dDataTab->ui->CD1_TabA1_1_10->setToolTip("");
    m_dDataTab->ui->CD1_TabA1_2_10->setToolTip("");
    m_dDataTab->ui->CD1_TabA1_3_10->setToolTip("");
    m_dDataTab->ui->CD1_TabA1_4_10->setToolTip("");
    m_dDataTab->ui->CD1_TabA1_5_10->setToolTip("");
    m_dDataTab->ui->CD1_TabA1_6_10->setToolTip("");



    QList<bool> bFlag;
    float* fAlgNG;
    float* fAlgOK;

    float* UpRatio;
    float* DownRatio;

    float Updata;
    float DownData;
    QString strUpRatio;
    QString strDownRatio;


    QGroupBox* pLabelTab;
    QLabel* pLabel;


    switch (m_nDefctType) {
    case 1:
        // 缺陷：方形 测量值
        m_dDataTab->ui->CD1_TabA2_1_7->setText(QString::number(m_tAlg->m_worker->m_dD_R_DataA.dExternal));
        m_dDataTab->ui->CD1_TabA2_2_7->setText(QString::number(m_tAlg->m_worker->m_dD_R_DataA.dInterior));


        m_dDataTab->ui->CD1_TabA2_1_9->setText(QString::number(m_tAlg->m_worker->m_nAlgOK.dD_R_DataA.dExternal));
        m_dDataTab->ui->CD1_TabA2_1_10->setText(QString::number(m_tAlg->m_worker->m_nAlgNG.dD_R_DataA.dExternal));


        m_dDataTab->ui->CD1_TabA2_2_9->setText(QString::number(m_tAlg->m_worker->m_nAlgOK.dD_R_DataA.dInterior));
        m_dDataTab->ui->CD1_TabA2_2_10->setText(QString::number(m_tAlg->m_worker->m_nAlgNG.dD_R_DataA.dInterior));

        m_dDataTab->ui->CD1_TabA2_1_10->setToolTip(QString::fromLocal8Bit("不确定：%1").arg(m_tAlg->m_worker->m_nDefectAbn));

        m_dDataTab->ui->CD1_TabA2_2_10->setToolTip(QString::fromLocal8Bit("不确定：%1").arg(m_tAlg->m_worker->m_nDefectAbn));

        m_csData.csNum1Unknown = QString::number(m_tAlg->m_worker->m_nDefectAbn);
        m_csData.csNum2Unknown = QString::number(m_tAlg->m_worker->m_nDefectAbn);
        break;
    case 2:
        // 缺陷：扇形 测量值
        m_dDataTab->ui->CD1_TabA2_1_7->setText(QString::number(m_tAlg->m_worker->m_dD_S_DataA.dExternal));
        m_dDataTab->ui->CD1_TabA2_2_7->setText(QString::number(m_tAlg->m_worker->m_dD_S_DataA.dInterior));

        m_dDataTab->ui->CD1_TabA1_1_9->setText(QString::number(m_tAlg->m_worker->m_nAlgOK.dD_S_DataA.dExternal));
        m_dDataTab->ui->CD1_TabA1_1_10->setText(QString::number(m_tAlg->m_worker->m_nAlgNG.dD_S_DataA.dExternal));


        m_dDataTab->ui->CD1_TabA1_2_9->setText(QString::number(m_tAlg->m_worker->m_nAlgOK.dD_S_DataA.dInterior));
        m_dDataTab->ui->CD1_TabA1_2_10->setText(QString::number(m_tAlg->m_worker->m_nAlgNG.dD_S_DataA.dInterior));

        m_dDataTab->ui->CD1_TabA1_1_10->setToolTip(QString::fromLocal8Bit("不确定：%1").arg(m_tAlg->m_worker->m_nDefectAbn));

        m_dDataTab->ui->CD1_TabA1_2_10->setToolTip(QString::fromLocal8Bit("不确定：%1").arg(m_tAlg->m_worker->m_nDefectAbn));

        m_csData.csNum1Unknown = QString::number(m_tAlg->m_worker->m_nDefectAbn);
        m_csData.csNum2Unknown = QString::number(m_tAlg->m_worker->m_nDefectAbn);

        break;
    default:

        break;
    }


    switch (m_nRectType) {
    case 1:
        // 正/长方形 全检
        // 测量值
        m_dDataTab->ui->CD1_TabA1_1_7->setText(QString::number(m_tAlg->m_worker->m_dM_R_A.dWidth));
        m_dDataTab->ui->CD1_TabA1_2_7->setText(QString::number(m_tAlg->m_worker->m_dM_R_A.dLength));
        m_dDataTab->ui->CD1_TabA1_3_7->setText(QString::number(m_tAlg->m_worker->m_dM_R_A.dAngle1));
        m_dDataTab->ui->CD1_TabA1_4_7->setText(QString::number(m_tAlg->m_worker->m_dM_R_A.dAngle2));
        m_dDataTab->ui->CD1_TabA1_5_7->setText(QString::number(m_tAlg->m_worker->m_dM_R_A.dAngle3));
        m_dDataTab->ui->CD1_TabA1_6_7->setText(QString::number(m_tAlg->m_worker->m_dM_R_A.dAngle4));


        //垂直方向长度
        if (!type){
            if (!m_tAlg->m_worker->m_measureRect.Flag1)
            {
                m_dDataTab->ui->ccd_Atab_1->setStyleSheet("background-color: rgb(255, 0, 0);");
                m_dDataTab->ui->CD1_TabA1_1_8->setText("NG");
            }
            else{
                m_dDataTab->ui->ccd_Atab_1->setStyleSheet("background-color:transparent;");
                m_dDataTab->ui->CD1_TabA1_1_8->setText("OK");
            }

            //水平方向长度
            if (!m_tAlg->m_worker->m_measureRect.Flag3)
            {
                m_dDataTab->ui->ccd_Atab_2->setStyleSheet("background-color: rgb(255, 0, 0);");
                m_dDataTab->ui->CD1_TabA1_2_8->setText("NG");
            }
            else{
                m_dDataTab->ui->ccd_Atab_2->setStyleSheet("background-color:transparent;");
                m_dDataTab->ui->CD1_TabA1_2_8->setText("OK");
            }
        }

        m_dDataTab->ui->CD1_TabA1_1_9->setText(QString::number(m_tAlg->m_worker->m_nAlgOK.dM_R_DataA.dWidth));
        m_dDataTab->ui->CD1_TabA1_2_9->setText(QString::number(m_tAlg->m_worker->m_nAlgOK.dM_R_DataA.dLength));
        m_dDataTab->ui->CD1_TabA1_1_10->setText(QString::number(m_tAlg->m_worker->m_nAlgNG.dM_R_DataA.dWidth));
        m_dDataTab->ui->CD1_TabA1_2_10->setText(QString::number(m_tAlg->m_worker->m_nAlgNG.dM_R_DataA.dLength));


        m_csData.csNum1 = QString::number(m_tAlg->m_worker->m_nAlgOK.dM_R_DataA.dWidth);
        m_csData.csNum2 = QString::number(m_tAlg->m_worker->m_nAlgNG.dM_R_DataA.dWidth);


        m_csData.csNum3 = QString::number(m_tAlg->m_worker->m_nAlgOK.dM_R_DataA.dLength);
        m_csData.csNum4 = QString::number(m_tAlg->m_worker->m_nAlgNG.dM_R_DataA.dLength);



        UpRatio = &m_tAlg->m_worker->m_nAlgUp.dM_R_DataA.dWidth;;
        DownRatio = &m_tAlg->m_worker->m_nAlgDown.dM_R_DataA.dWidth;

        Updata = *UpRatio / (*UpRatio + *DownRatio);
        DownData = *DownRatio / (*UpRatio + *DownRatio);

        strUpRatio = QString("%1%2").arg(QString::number(Updata * 100, 'f', 2)).arg("%");
        strDownRatio = QString("%1%2").arg(QString::number(DownData * 100, 'f', 2)).arg("%");

        m_dDataTab->ui->CD1_TabA1_1_10->setToolTip(QString::fromLocal8Bit("超上公差: %1 %2 ;\n"
                                                                          "超下公差: %3 %4 ;\n不确定：%5").arg(*UpRatio).arg(strUpRatio).arg(*DownRatio).arg(strDownRatio).arg(m_tAlg->m_worker->m_nRectAbn));

        m_csData.csNum1Up = QString::number(*UpRatio);
        m_csData.csNum1UpRatio = strUpRatio;
        m_csData.csNum1Down = QString::number(*DownRatio);
        m_csData.csNum1DownRatio = strDownRatio;
        m_csData.csNum1Unknown = QString::number(m_tAlg->m_worker->m_nRectAbn);

        UpRatio = &m_tAlg->m_worker->m_nAlgUp.dM_R_DataA.dLength;;
        DownRatio = &m_tAlg->m_worker->m_nAlgDown.dM_R_DataA.dLength;

        Updata = *UpRatio / (*UpRatio + *DownRatio);
        DownData = *DownRatio / (*UpRatio + *DownRatio);

        strUpRatio = QString("%1%2").arg(QString::number(Updata * 100, 'f', 2)).arg("%");
        strDownRatio = QString("%1%2").arg(QString::number(DownData * 100, 'f', 2)).arg("%");

        m_dDataTab->ui->CD1_TabA1_2_10->setToolTip(QString::fromLocal8Bit("超上公差: %1 %2 ;\n"
                                                                          "超下公差: %3 %4 ;\n不确定：%5").arg(*UpRatio).arg(strUpRatio).arg(*DownRatio).arg(strDownRatio).arg(m_tAlg->m_worker->m_nRectAbn));

        m_csData.csNum2Up = QString::number(*UpRatio);
        m_csData.csNum2UpRatio = strUpRatio;
        m_csData.csNum2Down = QString::number(*DownRatio);
        m_csData.csNum2DownRatio = strDownRatio;
        m_csData.csNum2Unknown = QString::number(m_tAlg->m_worker->m_nRectAbn);

        // 角度1  2 3 4

        bFlag << m_tAlg->m_worker->m_measureRect.Flag5 << m_tAlg->m_worker->m_measureRect.Flag6
              << m_tAlg->m_worker->m_measureRect.Flag7 << m_tAlg->m_worker->m_measureRect.Flag8;

        fAlgNG = &m_tAlg->m_worker->m_nAlgNG.dM_R_DataA.dAngle1;
        fAlgOK = &m_tAlg->m_worker->m_nAlgOK.dM_R_DataA.dAngle1;

        UpRatio = &m_tAlg->m_worker->m_nAlgUp.dM_R_DataA.dAngle1;;
        DownRatio = &m_tAlg->m_worker->m_nAlgDown.dM_R_DataA.dAngle1;

        for (int i = 0; i < 4; i++)
        {
            pLabelTab = this->findChild<QGroupBox*>(QString("ccd_Atab_%1").arg(QString::number(i + 3)));
            pLabel = this->findChild<QLabel*>(QString("CD1_TabA1_%1_8").arg(QString::number(i + 3)));
            if (!type)
            {
                if (!bFlag[i])
                {
                    pLabelTab->setStyleSheet("background-color: rgb(255, 0, 0);");
                    pLabel->setText("NG");
                }
                else
                {
                    pLabelTab->setStyleSheet("background-color:transparent;");
                    pLabel->setText("OK");
                }
            }
            pLabel = this->findChild<QLabel*>(QString("CD1_TabA1_%1_9").arg(QString::number(i + 3)));
            pLabel->setText(QString::number(*fAlgOK));
            pLabel = this->findChild<QLabel*>(QString("CD1_TabA1_%1_10").arg(QString::number(i + 3)));
            pLabel->setText(QString::number(*fAlgNG));


            Updata = *UpRatio / (*UpRatio + *DownRatio);
            DownData = *DownRatio / (*UpRatio + *DownRatio);

            strUpRatio = QString("%1%2").arg(QString::number(Updata * 100, 'f', 2)).arg("%");
            strDownRatio = QString("%1%2").arg(QString::number(DownData * 100, 'f', 2)).arg("%");

            pLabel->setToolTip(QString::fromLocal8Bit("超上公差: %1 %2 ;\n"
                                                      "超下公差: %3 %4 ;\n不确定：%5").arg(*UpRatio).arg(strUpRatio).arg(*DownRatio).arg(strDownRatio).arg(m_tAlg->m_worker->m_nRectAbn));

            switch (i) {
            case 0:
                m_csData.csAngle1OK = QString::number(*fAlgOK);
                m_csData.csAngle1NG = QString::number(*fAlgNG);
                m_csData.csAngle1Up = QString::number(*UpRatio);
                m_csData.csAngle1UpRatio = strUpRatio;
                m_csData.csAngle1Down = QString::number(*DownRatio);
                m_csData.csAngle1DownRatio = strDownRatio;
                m_csData.csAngle1Unknown = QString::number(m_tAlg->m_worker->m_nRectAbn);
                break;
            case 1:
                m_csData.csAngle2OK = QString::number(*fAlgOK);
                m_csData.csAngle2NG = QString::number(*fAlgNG);
                m_csData.csAngle2Up = QString::number(*UpRatio);
                m_csData.csAngle2UpRatio = strUpRatio;
                m_csData.csAngle2Down = QString::number(*DownRatio);
                m_csData.csAngle2DownRatio = strDownRatio;
                m_csData.csAngle2Unknown = QString::number(m_tAlg->m_worker->m_nRectAbn);
                break;
            case 2:
                m_csData.csAngle3OK = QString::number(*fAlgOK);
                m_csData.csAngle3NG = QString::number(*fAlgNG);
                m_csData.csAngle3Up = QString::number(*UpRatio);
                m_csData.csAngle3UpRatio = strUpRatio;
                m_csData.csAngle3Down = QString::number(*DownRatio);
                m_csData.csAngle3DownRatio = strDownRatio;
                m_csData.csAngle3Unknown = QString::number(m_tAlg->m_worker->m_nRectAbn);
                break;
            case 3:
                m_csData.csAngle4OK = QString::number(*fAlgOK);
                m_csData.csAngle4NG = QString::number(*fAlgNG);
                m_csData.csAngle4Up = QString::number(*UpRatio);
                m_csData.csAngle4UpRatio = strUpRatio;
                m_csData.csAngle4Down = QString::number(*DownRatio);
                m_csData.csAngle4DownRatio = strDownRatio;
                m_csData.csAngle4Unknown = QString::number(m_tAlg->m_worker->m_nRectAbn);
                break;
            default:
                break;
            }




            fAlgNG++;
            fAlgOK++;
            UpRatio++;
            DownRatio++;

        }
        break;
    case 2: // 梯形
        // 测量值
        m_dDataTab->ui->CD1_TabA1_1_7->setText(QString::number(m_tAlg->m_worker->m_dM_T_A.dHeight));
        m_dDataTab->ui->CD1_TabA1_2_7->setText(QString::number(m_tAlg->m_worker->m_dM_T_A.dLefthy));
        m_dDataTab->ui->CD1_TabA1_3_7->setText(QString::number(m_tAlg->m_worker->m_dM_T_A.dRighthy));




        if (!type)
        {
            //梯形高度
            if (!m_tAlg->m_worker->m_measureTrapezoid.Flag1)
            {
                m_dDataTab->ui->ccd_Atab_1->setStyleSheet("background-color: rgb(255, 0, 0);");
                m_dDataTab->ui->CD1_TabA1_1_8->setText("NG");
            }
            else{
                m_dDataTab->ui->ccd_Atab_1->setStyleSheet("background-color:transparent;");
                m_dDataTab->ui->CD1_TabA1_1_8->setText("OK");
            }


            //斜边角度1
            if (!m_tAlg->m_worker->m_measureTrapezoid.Flag3)
            {
                m_dDataTab->ui->ccd_Atab_2->setStyleSheet("background-color: rgb(255, 0, 0);");
                m_dDataTab->ui->CD1_TabA1_2_8->setText("NG");
            }
            else{
                m_dDataTab->ui->ccd_Atab_2->setStyleSheet("background-color:transparent;");
                m_dDataTab->ui->CD1_TabA1_2_8->setText("OK");
            }

            //斜边角度2
            if (!m_tAlg->m_worker->m_measureTrapezoid.Flag4)
            {
                m_dDataTab->ui->ccd_Atab_3->setStyleSheet("background-color: rgb(255, 0, 0);");
                m_dDataTab->ui->CD1_TabA1_3_8->setText("NG");
            }
            else{
                m_dDataTab->ui->ccd_Atab_3->setStyleSheet("background-color:transparent;");
                m_dDataTab->ui->CD1_TabA1_3_8->setText("OK");
            }
        }
        m_dDataTab->ui->CD1_TabA1_1_9->setText(QString::number(m_tAlg->m_worker->m_nAlgOK.dM_T_DataA.dHeight));
        m_dDataTab->ui->CD1_TabA1_2_9->setText(QString::number(m_tAlg->m_worker->m_nAlgOK.dM_T_DataA.dLefthy));
        m_dDataTab->ui->CD1_TabA1_3_9->setText(QString::number(m_tAlg->m_worker->m_nAlgOK.dM_T_DataA.dRighthy));

        m_dDataTab->ui->CD1_TabA1_1_10->setText(QString::number(m_tAlg->m_worker->m_nAlgNG.dM_T_DataA.dHeight));
        m_dDataTab->ui->CD1_TabA1_2_10->setText(QString::number(m_tAlg->m_worker->m_nAlgNG.dM_T_DataA.dLefthy));
        m_dDataTab->ui->CD1_TabA1_3_10->setText(QString::number(m_tAlg->m_worker->m_nAlgNG.dM_T_DataA.dRighthy));



        m_csData.csNum1 = QString::number(m_tAlg->m_worker->m_nAlgOK.dM_T_DataA.dHeight);
        m_csData.csNum2 = QString::number(m_tAlg->m_worker->m_nAlgNG.dM_T_DataA.dHeight);


        m_csData.csNum3 = QString::number(m_tAlg->m_worker->m_nAlgOK.dM_T_DataA.dLefthy);
        m_csData.csNum4 = QString::number(m_tAlg->m_worker->m_nAlgNG.dM_T_DataA.dLefthy);

        m_csData.csNum5 = QString::number(m_tAlg->m_worker->m_nAlgOK.dM_T_DataA.dRighthy);
        m_csData.csNum6 = QString::number(m_tAlg->m_worker->m_nAlgNG.dM_T_DataA.dRighthy);

        break;
    case 3:
        // 厚度测试
        m_dDataTab->ui->CD1_TabA1_1_7->setText(QString::number(m_tAlg->m_worker->m_dM_R_B.dThickness));
        //垂直方向长度
        if (!type)
        {
            if (!m_tAlg->m_worker->m_measureSide.Flag1)
            {
                m_dDataTab->ui->ccd_Atab_1->setStyleSheet("background-color: rgb(255, 0, 0);");
                m_dDataTab->ui->CD1_TabA1_1_8->setText("NG");

            }
            else{
                m_dDataTab->ui->ccd_Atab_1->setStyleSheet("background-color:transparent;");
                m_dDataTab->ui->CD1_TabA1_1_8->setText("OK");
            }
        }

        m_dDataTab->ui->CD1_TabA1_1_9->setText(QString::number(m_tAlg->m_worker->m_nAlgOK.dM_R_DataB.dThickness));
        m_dDataTab->ui->CD1_TabA1_1_10->setText(QString::number(m_tAlg->m_worker->m_nAlgNG.dM_R_DataB.dThickness));


        m_csData.csNum5 = QString::number(m_tAlg->m_worker->m_nAlgOK.dM_R_DataB.dThickness);
        m_csData.csNum6 = QString::number(m_tAlg->m_worker->m_nAlgNG.dM_R_DataB.dThickness);


        UpRatio = &m_tAlg->m_worker->m_nAlgUp.dM_R_DataB.dThickness;;
        DownRatio = &m_tAlg->m_worker->m_nAlgDown.dM_R_DataB.dThickness;

        Updata = *UpRatio / (*UpRatio + *DownRatio);
        DownData = *DownRatio / (*UpRatio + *DownRatio);

        strUpRatio = QString("%1%2").arg(QString::number(Updata * 100, 'f', 2)).arg("%");
        strDownRatio = QString("%1%2").arg(QString::number(DownData * 100, 'f', 2)).arg("%");

        m_dDataTab->ui->CD1_TabA1_1_10->setToolTip(QString::fromLocal8Bit("超上公差: %1 %2 ;\n"
                                                                          "超下公差: %3 %4 ;\n不确定：%5").arg(*UpRatio).arg(strUpRatio).arg(*DownRatio).arg(strDownRatio).arg(m_tAlg->m_worker->m_nRectAbn));



        m_csData.csNum3Up = QString::number(*UpRatio);
        m_csData.csNum3UpRatio = strUpRatio;
        m_csData.csNum3Down = QString::number(*DownRatio);
        m_csData.csNum3DownRatio = strDownRatio;
        m_csData.csNum3Unknown = QString::number(m_tAlg->m_worker->m_nRectAbn);
        break;
    case 4:
        // 测量值  异形正面
        m_dDataTab->ui->CD1_TabA1_1_7->setText(QString::number(m_tAlg->m_worker->m_dM_TS_A.dHeight));
        m_dDataTab->ui->CD1_TabA1_2_7->setText(QString::number(m_tAlg->m_worker->m_dM_TS_A.dWidth));


        bFlag << m_tAlg->m_worker->m_measureTS.Flag1 << m_tAlg->m_worker->m_measureTS.Flag2;


        m_csData.csNum1 = QString::number(m_tAlg->m_worker->m_nAlgOK.dM_TS_DataA.dHeight);
        m_csData.csNum2 = QString::number(m_tAlg->m_worker->m_nAlgNG.dM_TS_DataA.dHeight);


        m_csData.csNum3 = QString::number(m_tAlg->m_worker->m_nAlgOK.dM_TS_DataA.dWidth);
        m_csData.csNum4 = QString::number(m_tAlg->m_worker->m_nAlgNG.dM_TS_DataA.dWidth);



        fAlgNG = &m_tAlg->m_worker->m_nAlgNG.dM_TS_DataA.dHeight;
        fAlgOK = &m_tAlg->m_worker->m_nAlgOK.dM_TS_DataA.dHeight;

        UpRatio = &m_tAlg->m_worker->m_nAlgUp.dM_TS_DataA.dHeight;;
        DownRatio = &m_tAlg->m_worker->m_nAlgDown.dM_TS_DataA.dHeight;
        for (int i = 0; i < 2; i++)
        {
            pLabelTab = this->findChild<QGroupBox*>(QString("ccd_Atab_%1").arg(QString::number(i + 1)));
            pLabel = this->findChild<QLabel*>(QString("CD1_TabA1_%1_8").arg(QString::number(i + 1)));
            if (!type)
            {
                if (!bFlag[i])
                {
                    pLabelTab->setStyleSheet("background-color: rgb(255, 0, 0);");
                    pLabel->setText("NG");
                }
                else
                {
                    pLabelTab->setStyleSheet("background-color:transparent;");
                    pLabel->setText("OK");
                }
            }
            pLabel = this->findChild<QLabel*>(QString("CD1_TabA1_%1_9").arg(QString::number(i + 1)));
            pLabel->setText(QString::number(*fAlgOK));
            pLabel = this->findChild<QLabel*>(QString("CD1_TabA1_%1_10").arg(QString::number(i + 1)));
            pLabel->setText(QString::number(*fAlgNG));


            Updata = *UpRatio / (*UpRatio + *DownRatio);
            DownData = *DownRatio / (*UpRatio + *DownRatio);

            strUpRatio = QString("%1%2").arg(QString::number(Updata * 100, 'f', 2)).arg("%");
            strDownRatio = QString("%1%2").arg(QString::number(DownData * 100, 'f', 2)).arg("%");

            pLabel->setToolTip(QString::fromLocal8Bit("超上公差: %1 %2 ;\n"
                                                      "超下公差: %3 %4 ;\n不确定：%5").arg(*UpRatio).arg(strUpRatio).arg(*DownRatio).arg(strDownRatio).arg(m_tAlg->m_worker->m_nRectAbn));
            if(i == 0){
                m_csData.csNum1Up = QString::number(*UpRatio);
                m_csData.csNum1UpRatio = strUpRatio;
                m_csData.csNum1Down = QString::number(*DownRatio);
                m_csData.csNum1DownRatio = strDownRatio;
                m_csData.csNum1Unknown = QString::number(m_tAlg->m_worker->m_nRectAbn);
            }
            if(i == 1){
                m_csData.csNum2Up = QString::number(*UpRatio);
                m_csData.csNum2UpRatio = strUpRatio;
                m_csData.csNum2Down = QString::number(*DownRatio);
                m_csData.csNum2DownRatio = strDownRatio;
                m_csData.csNum2Unknown = QString::number(m_tAlg->m_worker->m_nRectAbn);
            }


            fAlgNG++;
            fAlgOK++;
            UpRatio++;
            DownRatio++;
        }

        break;
    case 5:
        // 测量值 异形侧面
        m_dDataTab->ui->CD1_TabA1_1_7->setText(QString::number(m_tAlg->m_worker->m_dM_TSS_A.dThickness));

        m_csData.csNum5 = QString::number(m_tAlg->m_worker->m_nAlgOK.dM_TSS_DataA.dThickness);
        m_csData.csNum6 = QString::number(m_tAlg->m_worker->m_nAlgNG.dM_TSS_DataA.dThickness);

        bFlag << m_tAlg->m_worker->m_measureTSS.Flag;

        fAlgNG = &m_tAlg->m_worker->m_nAlgNG.dM_TSS_DataA.dThickness;
        fAlgOK = &m_tAlg->m_worker->m_nAlgOK.dM_TSS_DataA.dThickness;

        UpRatio = &m_tAlg->m_worker->m_nAlgUp.dM_TSS_DataA.dThickness;;
        DownRatio = &m_tAlg->m_worker->m_nAlgDown.dM_TSS_DataA.dThickness;
        for (int i = 0; i < 1; i++)
        {
            pLabelTab = this->findChild<QGroupBox*>(QString("ccd_Atab_%1").arg(QString::number(i + 1)));
            pLabel = this->findChild<QLabel*>(QString("CD1_TabA1_%1_8").arg(QString::number(i + 1)));
            if (!type)
            {
                if (!bFlag[i])
                {
                    pLabelTab->setStyleSheet("background-color: rgb(255, 0, 0);");
                    pLabel->setText("NG");
                }
                else
                {
                    pLabelTab->setStyleSheet("background-color:transparent;");
                    pLabel->setText("OK");
                }
            }
            pLabel = this->findChild<QLabel*>(QString("CD1_TabA1_%1_9").arg(QString::number(i + 1)));
            pLabel->setText(QString::number(*fAlgOK));
            pLabel = this->findChild<QLabel*>(QString("CD1_TabA1_%1_10").arg(QString::number(i + 1)));
            pLabel->setText(QString::number(*fAlgNG));


            Updata = *UpRatio / (*UpRatio + *DownRatio);
            DownData = *DownRatio / (*UpRatio + *DownRatio);

            strUpRatio = QString("%1%2").arg(QString::number(Updata * 100, 'f', 2)).arg("%");
            strDownRatio = QString("%1%2").arg(QString::number(DownData * 100, 'f', 2)).arg("%");

            pLabel->setToolTip(QString::fromLocal8Bit("超上公差: %1 %2 ;\n"
                                                      "超下公差: %3 %4 ;\n不确定：%5").arg(*UpRatio).arg(strUpRatio).arg(*DownRatio).arg(strDownRatio).arg(m_tAlg->m_worker->m_nRectAbn));

            m_csData.csNum3Up = QString::number(*UpRatio);
            m_csData.csNum3UpRatio = strUpRatio;
            m_csData.csNum3Down = QString::number(*DownRatio);
            m_csData.csNum3DownRatio = strDownRatio;
            m_csData.csNum3Unknown = QString::number(m_tAlg->m_worker->m_nRectAbn);
            fAlgNG++;
            fAlgOK++;
            UpRatio++;
            DownRatio++;
        }
        break;
    case 7:
        // 扇形 测量值
        m_dDataTab->ui->CD1_TabA1_1_7->setText(QString::number(m_tAlg->m_worker->m_dM_Se_A.dEdge1));
        m_dDataTab->ui->CD1_TabA1_2_7->setText(QString::number(m_tAlg->m_worker->m_dM_Se_A.dEdge2));
        m_dDataTab->ui->CD1_TabA1_3_7->setText(QString::number(m_tAlg->m_worker->m_dM_Se_A.dStraight3));
        m_dDataTab->ui->CD1_TabA1_4_7->setText(QString::number(m_tAlg->m_worker->m_dM_Se_A.dStraight4));
        m_dDataTab->ui->CD1_TabA1_5_7->setText(QString::number(m_tAlg->m_worker->m_dM_Se_A.dMStraight));
        m_dDataTab->ui->CD1_TabA1_6_7->setText(QString::number(m_tAlg->m_worker->m_dM_Se_A.dProfile));


        m_csData.csNum1 = QString::number(m_tAlg->m_worker->m_nAlgOK.dM_Se_DataA.dEdge1);
        m_csData.csNum2 = QString::number(m_tAlg->m_worker->m_nAlgNG.dM_Se_DataA.dEdge1);


        m_csData.csNum3 = QString::number(m_tAlg->m_worker->m_nAlgOK.dM_Se_DataA.dEdge2);
        m_csData.csNum4 = QString::number(m_tAlg->m_worker->m_nAlgNG.dM_Se_DataA.dEdge2);


        m_csData.csNum5 = QString::number(m_tAlg->m_worker->m_nAlgOK.dM_Se_DataA.dStraight3);
        m_csData.csNum6 = QString::number(m_tAlg->m_worker->m_nAlgNG.dM_Se_DataA.dStraight3);


        m_csData.csNum7 = QString::number(m_tAlg->m_worker->m_nAlgOK.dM_Se_DataA.dStraight4);
        m_csData.csNum8 = QString::number(m_tAlg->m_worker->m_nAlgNG.dM_Se_DataA.dStraight4);



        m_csData.csNum9 = QString::number(m_tAlg->m_worker->m_nAlgOK.dM_Se_DataA.dMStraight);
        m_csData.csNum10 = QString::number(m_tAlg->m_worker->m_nAlgNG.dM_Se_DataA.dMStraight);


        m_csData.csNum11 = QString::number(m_tAlg->m_worker->m_nAlgOK.dM_Se_DataA.dProfile);
        m_csData.csNum12 = QString::number(m_tAlg->m_worker->m_nAlgNG.dM_Se_DataA.dProfile);




        bFlag << m_tAlg->m_worker->m_measureSector.Flag1 << m_tAlg->m_worker->m_measureSector.Flag2
              << m_tAlg->m_worker->m_measureSector.Flag3 << m_tAlg->m_worker->m_measureSector.Flag4
              << m_tAlg->m_worker->m_measureSector.Flag5 << m_tAlg->m_worker->m_measureSector.Flag6;

        fAlgNG = &m_tAlg->m_worker->m_nAlgNG.dM_Se_DataA.dEdge1;
        fAlgOK = &m_tAlg->m_worker->m_nAlgOK.dM_Se_DataA.dEdge1;

        UpRatio = &m_tAlg->m_worker->m_nAlgUp.dM_Se_DataA.dEdge1;;
        DownRatio = &m_tAlg->m_worker->m_nAlgDown.dM_Se_DataA.dEdge1;
        for (int i = 0; i < 6; i++)
        {
            pLabelTab = this->findChild<QGroupBox*>(QString("ccd_Atab_%1").arg(QString::number(i + 1)));
            pLabel = this->findChild<QLabel*>(QString("CD1_TabA1_%1_8").arg(QString::number(i + 1)));
            if (!type)
            {
                if (!bFlag[i])
                {
                    pLabelTab->setStyleSheet("background-color: rgb(255, 0, 0);");
                    pLabel->setText("NG");
                }
                else
                {
                    pLabelTab->setStyleSheet("background-color:transparent;");
                    pLabel->setText("OK");
                }
            }
            pLabel = this->findChild<QLabel*>(QString("CD1_TabA1_%1_9").arg(QString::number(i + 1)));
            pLabel->setText(QString::number(*fAlgOK));
            pLabel = this->findChild<QLabel*>(QString("CD1_TabA1_%1_10").arg(QString::number(i + 1)));
            pLabel->setText(QString::number(*fAlgNG));


            Updata = *UpRatio / (*UpRatio + *DownRatio);
            DownData = *DownRatio / (*UpRatio + *DownRatio);

            strUpRatio = QString("%1%2").arg(QString::number(Updata * 100, 'f', 2)).arg("%");
            strDownRatio = QString("%1%2").arg(QString::number(DownData * 100, 'f', 2)).arg("%");

            pLabel->setToolTip(QString::fromLocal8Bit("超上公差: %1 %2 ;\n"
                                                      "超下公差: %3 %4 ;\n不确定：%5").arg(*UpRatio).arg(strUpRatio).arg(*DownRatio).arg(strDownRatio).arg(m_tAlg->m_worker->m_nRectAbn));
            switch (i) {
            case 0:
                m_csData.csNum1Up = QString::number(*UpRatio);
                m_csData.csNum1UpRatio = strUpRatio;
                m_csData.csNum1Down = QString::number(*DownRatio);
                m_csData.csNum1DownRatio = strDownRatio;
                m_csData.csNum1Unknown = QString::number(m_tAlg->m_worker->m_nRectAbn);
                break;
            case 1:
                m_csData.csNum2Up = QString::number(*UpRatio);
                m_csData.csNum2UpRatio = strUpRatio;
                m_csData.csNum2Down = QString::number(*DownRatio);
                m_csData.csNum2DownRatio = strDownRatio;
                m_csData.csNum2Unknown = QString::number(m_tAlg->m_worker->m_nRectAbn);
                break;
            case 2:
                m_csData.csNum3Up = QString::number(*UpRatio);
                m_csData.csNum3UpRatio = strUpRatio;
                m_csData.csNum3Down = QString::number(*DownRatio);
                m_csData.csNum3DownRatio = strDownRatio;
                m_csData.csNum3Unknown = QString::number(m_tAlg->m_worker->m_nRectAbn);
                break;
            case 3:
                m_csData.csNum4Up = QString::number(*UpRatio);
                m_csData.csNum4UpRatio = strUpRatio;
                m_csData.csNum4Down = QString::number(*DownRatio);
                m_csData.csNum4DownRatio = strDownRatio;
                m_csData.csNum4Unknown = QString::number(m_tAlg->m_worker->m_nRectAbn);
                break;
            case 4:
                m_csData.csNum5Up = QString::number(*UpRatio);
                m_csData.csNum5UpRatio = strUpRatio;
                m_csData.csNum5Down = QString::number(*DownRatio);
                m_csData.csNum5DownRatio = strDownRatio;
                m_csData.csNum5Unknown = QString::number(m_tAlg->m_worker->m_nRectAbn);
                break;
            case 5:
                m_csData.csNum6Up = QString::number(*UpRatio);
                m_csData.csNum6UpRatio = strUpRatio;
                m_csData.csNum6Down = QString::number(*DownRatio);
                m_csData.csNum6DownRatio = strDownRatio;
                m_csData.csNum6Unknown = QString::number(m_tAlg->m_worker->m_nRectAbn);
                break;
            default:
                break;
            }

            fAlgNG++;
            fAlgOK++;
            UpRatio++;
            DownRatio++;
        }
        break;
    case 8:
        // 子弹头
        // 测量值
        m_dDataTab->ui->CD1_TabA1_1_7->setText(QString::number(m_tAlg->m_worker->m_dM_Bullet_A.dWidth));
        m_dDataTab->ui->CD1_TabA1_2_7->setText(QString::number(m_tAlg->m_worker->m_dM_Bullet_A.dLength));
        m_dDataTab->ui->CD1_TabA1_3_7->setText(QString::number(m_tAlg->m_worker->m_dM_Bullet_A.dAngle1));
        m_dDataTab->ui->CD1_TabA1_4_7->setText(QString::number(m_tAlg->m_worker->m_dM_Bullet_A.dAngle2));



        m_csData.csNum1 = QString::number(m_tAlg->m_worker->m_nAlgOK.dM_Bullet_DataA.dWidth);
        m_csData.csNum2 = QString::number(m_tAlg->m_worker->m_nAlgNG.dM_Bullet_DataA.dWidth);


        m_csData.csNum3 = QString::number(m_tAlg->m_worker->m_nAlgOK.dM_Bullet_DataA.dLength);
        m_csData.csNum4 = QString::number(m_tAlg->m_worker->m_nAlgNG.dM_Bullet_DataA.dLength);


        m_csData.csNum5 = QString::number(m_tAlg->m_worker->m_nAlgOK.dM_Bullet_DataA.dAngle1);
        m_csData.csNum6 = QString::number(m_tAlg->m_worker->m_nAlgNG.dM_Bullet_DataA.dAngle1);


        m_csData.csNum7 = QString::number(m_tAlg->m_worker->m_nAlgOK.dM_Bullet_DataA.dAngle2);
        m_csData.csNum8 = QString::number(m_tAlg->m_worker->m_nAlgNG.dM_Bullet_DataA.dAngle2);


        bFlag << m_tAlg->m_worker->m_measureBullet.Flag1 << m_tAlg->m_worker->m_measureBullet.Flag2
              << m_tAlg->m_worker->m_measureBullet.Flag3 << m_tAlg->m_worker->m_measureBullet.Flag4;

        fAlgNG = &m_tAlg->m_worker->m_nAlgNG.dM_Bullet_DataA.dWidth;
        fAlgOK = &m_tAlg->m_worker->m_nAlgOK.dM_Bullet_DataA.dWidth;

        UpRatio = &m_tAlg->m_worker->m_nAlgUp.dM_Bullet_DataA.dWidth;;
        DownRatio = &m_tAlg->m_worker->m_nAlgDown.dM_Bullet_DataA.dWidth;
        for (int i = 0; i < 4; i++)
        {
            pLabelTab = this->findChild<QGroupBox*>(QString("ccd_Atab_%1").arg(QString::number(i + 1)));
            pLabel = this->findChild<QLabel*>(QString("CD1_TabA1_%1_8").arg(QString::number(i + 1)));
            if (!type)
            {
                if (!bFlag[i])
                {
                    pLabelTab->setStyleSheet("background-color: rgb(255, 0, 0);");
                    pLabel->setText("NG");
                }
                else
                {
                    pLabelTab->setStyleSheet("background-color:transparent;");
                    pLabel->setText("OK");
                }
            }
            pLabel = this->findChild<QLabel*>(QString("CD1_TabA1_%1_9").arg(QString::number(i + 1)));
            pLabel->setText(QString::number(*fAlgOK));
            pLabel = this->findChild<QLabel*>(QString("CD1_TabA1_%1_10").arg(QString::number(i + 1)));
            pLabel->setText(QString::number(*fAlgNG));


            Updata = *UpRatio / (*UpRatio + *DownRatio);
            DownData = *DownRatio / (*UpRatio + *DownRatio);

            strUpRatio = QString("%1%2").arg(QString::number(Updata * 100, 'f', 2)).arg("%");
            strDownRatio = QString("%1%2").arg(QString::number(DownData * 100, 'f', 2)).arg("%");

            pLabel->setToolTip(QString::fromLocal8Bit("超上公差: %1 %2 ;\n"
                                                      "超下公差: %3 %4 ;\n不确定：%5").arg(*UpRatio).arg(strUpRatio).arg(*DownRatio).arg(strDownRatio).arg(m_tAlg->m_worker->m_nRectAbn));
            if(i == 0){
                m_csData.csNum1Up = QString::number(*UpRatio);
                m_csData.csNum1UpRatio = strUpRatio;
                m_csData.csNum1Down = QString::number(*DownRatio);
                m_csData.csNum1DownRatio = strDownRatio;
                m_csData.csNum1Unknown = QString::number(m_tAlg->m_worker->m_nRectAbn);
            }
            if(i == 1){
                m_csData.csNum2Up = QString::number(*UpRatio);
                m_csData.csNum2UpRatio = strUpRatio;
                m_csData.csNum2Down = QString::number(*DownRatio);
                m_csData.csNum2DownRatio = strDownRatio;
                m_csData.csNum2Unknown = QString::number(m_tAlg->m_worker->m_nRectAbn);
            }
            fAlgNG++;
            fAlgOK++;
            UpRatio++;
            DownRatio++;
        }
        break;
    default:
        break;
    }
}

void MainWindow::RdProductIni(QString iniFilePath)
{
    QString Path = "Config/Product/" + iniFilePath;
    QSettings iniRead(Path, QSettings::IniFormat);
    iniRead.setIniCodec(QTextCodec::codecForName("UTF_8"));


    // 获取 设置产品 规格类型
    int nType = iniRead.value("Type/type").toInt();
    QString strSize = iniRead.value("Type/size").toString();
    m_strName = iniRead.value("Type/name").toString();
    m_tCsv->m_worker->m_nType = nType;



    ui->Main_ComboxProduct->setCurrentText(m_strName);

    if (nType == 3){
        m_dPlc->m_tPlc->m_worker->writePlcInfo(CUT_STATE, 2);
    }
    else{
        m_dPlc->m_tPlc->m_worker->writePlcInfo(CUT_STATE, 1);
    }
    ui->Main_ComboxType->setCurrentIndex(nType);
    ui->Main_EditSize->setText(strSize);

    m_csData.csName = m_strName;
    m_csData.csType = ui->Main_ComboxType->currentText();
    m_csData.csSize = strSize;
    // 获取检测参数
    float* fBullet1 = &m_tAlg1->m_worker->m_measureBullet.m_part.para1;
    float* fBullet2 = &m_tAlg2->m_worker->m_measureBullet.m_part.para1;
    float* fBullet3 = &m_tAlg3->m_worker->m_measureBullet.m_part.para1;
    float* fBullet4 = &m_tAlg4->m_worker->m_measureBullet.m_part.para1;
    float* fBullet5 = &m_tAlg5->m_worker->m_measureBullet.m_part.para1;
    float* fBullet6 = &m_tAlg6->m_worker->m_measureBullet.m_part.para1;
    float* fBullet7 = &m_tAlg7->m_worker->m_measureBullet.m_part.para1;
    float* fBullet8 = &m_tAlg8->m_worker->m_measureBullet.m_part.para1;



    float* fRect1 = &m_tAlg1->m_worker->m_measureRect.m_part.para1;
    float* fRect2 = &m_tAlg2->m_worker->m_measureRect.m_part.para1;
    float* fRect3 = &m_tAlg3->m_worker->m_measureRect.m_part.para1;
    float* fRect4 = &m_tAlg4->m_worker->m_measureRect.m_part.para1;
    float* fRect5 = &m_tAlg5->m_worker->m_measureRect.m_part.para1;
    float* fRect6 = &m_tAlg6->m_worker->m_measureRect.m_part.para1;
    float* fRect7 = &m_tAlg7->m_worker->m_measureRect.m_part.para1;
    float* fRect8 = &m_tAlg8->m_worker->m_measureRect.m_part.para1;


    float* fTrapezoid1 = &m_tAlg1->m_worker->m_measureTrapezoid.m_part.para1;
    float* fTrapezoid2 = &m_tAlg2->m_worker->m_measureTrapezoid.m_part.para1;
    float* fTrapezoid3 = &m_tAlg3->m_worker->m_measureTrapezoid.m_part.para1;
    float* fTrapezoid4 = &m_tAlg4->m_worker->m_measureTrapezoid.m_part.para1;
    float* fTrapezoid5 = &m_tAlg5->m_worker->m_measureTrapezoid.m_part.para1;
    float* fTrapezoid6 = &m_tAlg6->m_worker->m_measureTrapezoid.m_part.para1;
    float* fTrapezoid7 = &m_tAlg7->m_worker->m_measureTrapezoid.m_part.para1;
    float* fTrapezoid8 = &m_tAlg8->m_worker->m_measureTrapezoid.m_part.para1;

    float* fRectTS1 = &m_tAlg1->m_worker->m_measureTS.m_part.para1;
    float* fRectTS2 = &m_tAlg2->m_worker->m_measureTS.m_part.para1;
    float* fRectTS3 = &m_tAlg3->m_worker->m_measureTS.m_part.para1;
    float* fRectTS4 = &m_tAlg4->m_worker->m_measureTS.m_part.para1;
    float* fRectTS5 = &m_tAlg5->m_worker->m_measureTS.m_part.para1;
    float* fRectTS6 = &m_tAlg6->m_worker->m_measureTS.m_part.para1;
    float* fRectTS7 = &m_tAlg7->m_worker->m_measureTS.m_part.para1;
    float* fRectTS8 = &m_tAlg8->m_worker->m_measureTS.m_part.para1;


    float* fRectTSS1 = &m_tAlg1->m_worker->m_measureTSS.m_part.para1;
    float* fRectTSS2 = &m_tAlg2->m_worker->m_measureTSS.m_part.para1;
    float* fRectTSS3 = &m_tAlg3->m_worker->m_measureTSS.m_part.para1;
    float* fRectTSS4 = &m_tAlg4->m_worker->m_measureTSS.m_part.para1;
    float* fRectTSS5 = &m_tAlg5->m_worker->m_measureTSS.m_part.para1;
    float* fRectTSS6 = &m_tAlg6->m_worker->m_measureTSS.m_part.para1;
    float* fRectTSS7 = &m_tAlg7->m_worker->m_measureTSS.m_part.para1;
    float* fRectTSS8 = &m_tAlg8->m_worker->m_measureTSS.m_part.para1;




    float* fSide1 = &m_tAlg1->m_worker->m_measureSide.m_part.para1;
    float* fSide2 = &m_tAlg2->m_worker->m_measureSide.m_part.para1;
    float* fSide3 = &m_tAlg3->m_worker->m_measureSide.m_part.para1;
    float* fSide4 = &m_tAlg4->m_worker->m_measureSide.m_part.para1;
    float* fSide5 = &m_tAlg5->m_worker->m_measureSide.m_part.para1;
    float* fSide6 = &m_tAlg6->m_worker->m_measureSide.m_part.para1;
    float* fSide7 = &m_tAlg7->m_worker->m_measureSide.m_part.para1;
    float* fSide8 = &m_tAlg8->m_worker->m_measureSide.m_part.para1;

    HalconCpp::HTuple* fAlgPlace1 = &m_tAlg1->m_worker->m_algPlace.x1;
    HalconCpp::HTuple* fAlgPlace2 = &m_tAlg2->m_worker->m_algPlace.x1;
    HalconCpp::HTuple* fAlgPlace3 = &m_tAlg3->m_worker->m_algPlace.x1;
    HalconCpp::HTuple* fAlgPlace4 = &m_tAlg4->m_worker->m_algPlace.x1;
    HalconCpp::HTuple* fAlgPlace5 = &m_tAlg5->m_worker->m_algPlace.x1;
    HalconCpp::HTuple* fAlgPlace6 = &m_tAlg6->m_worker->m_algPlace.x1;
    HalconCpp::HTuple* fAlgPlace7 = &m_tAlg7->m_worker->m_algPlace.x1;
    HalconCpp::HTuple* fAlgPlace8 = &m_tAlg8->m_worker->m_algPlace.x1;

    float* fSector1 = &m_tAlg1->m_worker->m_measureSector.m_part.para1;
    float* fSector2 = &m_tAlg2->m_worker->m_measureSector.m_part.para1;
    float* fSector3 = &m_tAlg3->m_worker->m_measureSector.m_part.para1;
    float* fSector4 = &m_tAlg4->m_worker->m_measureSector.m_part.para1;
    float* fSector5 = &m_tAlg5->m_worker->m_measureSector.m_part.para1;
    float* fSector6 = &m_tAlg6->m_worker->m_measureSector.m_part.para1;
    float* fSector7 = &m_tAlg7->m_worker->m_measureSector.m_part.para1;
    float* fSector8 = &m_tAlg8->m_worker->m_measureSector.m_part.para1;



    int* nIndex1 = &m_tAlg1->m_worker->m_defectRect.m_part.para1;
    int* nIndex2 = &m_tAlg2->m_worker->m_defectRect.m_part.para1;
    int* nIndex3 = &m_tAlg3->m_worker->m_defectRect.m_part.para1;
    int* nIndex4 = &m_tAlg4->m_worker->m_defectRect.m_part.para1;
    int* nIndex5 = &m_tAlg5->m_worker->m_defectRect.m_part.para1;
    int* nIndex6 = &m_tAlg6->m_worker->m_defectRect.m_part.para1;
    int* nIndex7 = &m_tAlg7->m_worker->m_defectRect.m_part.para1;
    int* nIndex8 = &m_tAlg8->m_worker->m_defectRect.m_part.para1;

    float* fDSector1 = &m_tAlg1->m_worker->m_defectSector.m_part.para1;
    float* fDSector2 = &m_tAlg2->m_worker->m_defectSector.m_part.para1;
    float* fDSector3 = &m_tAlg3->m_worker->m_defectSector.m_part.para1;
    float* fDSector4 = &m_tAlg4->m_worker->m_defectSector.m_part.para1;
    float* fDSector5 = &m_tAlg5->m_worker->m_defectSector.m_part.para1;
    float* fDSector6 = &m_tAlg6->m_worker->m_defectSector.m_part.para1;
    float* fDSector7 = &m_tAlg7->m_worker->m_defectSector.m_part.para1;
    float* fDSector8 = &m_tAlg8->m_worker->m_defectSector.m_part.para1;



    float* fDSide1 = &m_tAlg1->m_worker->m_defectSide.m_part.para1;
    float* fDSide2 = &m_tAlg2->m_worker->m_defectSide.m_part.para1;
    float* fDSide3 = &m_tAlg3->m_worker->m_defectSide.m_part.para1;
    float* fDSide4 = &m_tAlg4->m_worker->m_defectSide.m_part.para1;
    float* fDSide5 = &m_tAlg5->m_worker->m_defectSide.m_part.para1;
    float* fDSide6 = &m_tAlg6->m_worker->m_defectSide.m_part.para1;
    float* fDSide7 = &m_tAlg7->m_worker->m_defectSide.m_part.para1;
    float* fDSide8 = &m_tAlg8->m_worker->m_defectSide.m_part.para1;

    int* nDFback1 = &m_tAlg1->m_worker->m_defectFback.m_part.para1;
    int* nDFback2 = &m_tAlg2->m_worker->m_defectFback.m_part.para1;
    int* nDFback3 = &m_tAlg3->m_worker->m_defectFback.m_part.para1;
    int* nDFback4 = &m_tAlg4->m_worker->m_defectFback.m_part.para1;
    int* nDFback5 = &m_tAlg5->m_worker->m_defectFback.m_part.para1;
    int* nDFback6 = &m_tAlg6->m_worker->m_defectFback.m_part.para1;
    int* nDFback7 = &m_tAlg7->m_worker->m_defectFback.m_part.para1;
    int* nDFback8 = &m_tAlg8->m_worker->m_defectFback.m_part.para1;



    QList<HalconCpp::HTuple*> m_lAlgPlace = QList<HalconCpp::HTuple*>() << fAlgPlace1 << fAlgPlace2 << fAlgPlace3 << fAlgPlace4
                                                                        << fAlgPlace5 << fAlgPlace6 << fAlgPlace7 << fAlgPlace8;


    QList<int*> m_lDFback = QList<int*>() << nDFback1 << nDFback2 << nDFback3 << nDFback4
                                          << nDFback5 << nDFback6 << nDFback7 << nDFback8;

    QList<int*> m_lDefect = QList<int*>() << nIndex1 << nIndex2 << nIndex3 << nIndex4
                                          << nIndex5 << nIndex6 << nIndex7 << nIndex8;
    QList<float*> m_lDSector = QList<float*>() << fDSector1 << fDSector2 << fDSector3 << fDSector4
                                               << fDSector5 << fDSector6 << fDSector7 << fDSector8;
    QList<float*> m_lDSide = QList<float*>() << fDSide1 << fDSide2 << fDSide3 << fDSide4
                                             << fDSide5 << fDSide6 << fDSide7 << fDSide8;


    QList<float*> m_lRect = QList<float*>() << fRect1 << fRect2 << fRect3 << fRect4
                                            << fRect5 << fRect6 << fRect7 << fRect8;
    QList<float*> m_lSide = QList<float*>() << fSide1 << fSide2 << fSide3 << fSide4
                                            << fSide5 << fSide6 << fSide7 << fSide8;
    QList<float*> m_lRectTS = QList<float*>() << fRectTS1 << fRectTS2 << fRectTS3 << fRectTS4
                                              << fRectTS5 << fRectTS6 << fRectTS7 << fRectTS8;
    QList<float*> m_lRectTSS = QList<float*>() << fRectTSS1 << fRectTSS2 << fRectTSS3 << fRectTSS4
                                               << fRectTSS5 << fRectTSS6 << fRectTSS7 << fRectTSS8;
    QList<float*> m_lTrapezoid = QList<float*>() << fTrapezoid1 << fTrapezoid2 << fTrapezoid3 << fTrapezoid4
                                                 << fTrapezoid5 << fTrapezoid6 << fTrapezoid7 << fTrapezoid8;
    QList<float*> m_lSector = QList<float*>() << fSector1 << fSector2 << fSector3 << fSector4
                                              << fSector5 << fSector6 << fSector7 << fSector8;


    QList<AlgThread*> m_lAlg = QList<AlgThread*>() << m_tAlg1 << m_tAlg2 << m_tAlg3 << m_tAlg4
                                                   << m_tAlg5 << m_tAlg6 << m_tAlg7 << m_tAlg8;

    QList<float*> m_lBullet = QList<float*>() << fBullet1 << fBullet2 << fBullet3 << fBullet4
                                              << fBullet5 << fBullet6 << fBullet7 << fBullet8;

    QString path;
    HalconCpp::HTuple ModelID;
    for (int i = 0; i < 8; i++){
        for (int j = 0; j < 10; j++){
            *m_lDFback[i] = iniRead.value(QString("DFback_%1/parame%2").arg(i + 1).arg(j)).toInt();
            m_lDFback[i]++;
        }


        for (int j = 0; j < 8; j++){
            *m_lDSector[i] = iniRead.value(QString("DSector_%1/parame%2").arg(i + 1).arg(j)).toFloat();
            m_lDSector[i]++;
        }

        for (int j = 0; j < 12; j++){
            *m_lDSide[i] = iniRead.value(QString("DSide_%1/parame%2").arg(i + 1).arg(j)).toFloat();
            m_lDSide[i]++;
        }


        for (int j = 0; j < 24; j++){
            *m_lSector[i] = iniRead.value(QString("Sector_%1/parame%2").arg(i + 1).arg(j)).toFloat();
            m_lSector[i]++;
        }

        for (int j = 0; j < 19; j++){
            *m_lTrapezoid[i] = iniRead.value(QString("Trapezoid_%1/parame%2").arg(i + 1).arg(j)).toFloat();
            m_lTrapezoid[i]++;
        }
        for (int j = 0; j < 36; j++){
            *m_lRect[i] = iniRead.value(QString("Rect_%1/parame%2").arg(i + 1).arg(j)).toFloat();
            m_lRect[i]++;
        }


        for (int j = 0; j < 36; j++){
            *m_lBullet[i] = iniRead.value(QString("Bullet_%1/parame%2").arg(i + 1).arg(j)).toFloat();
            m_lBullet[i]++;
        }

        for (int j = 0; j < 30; j++){
            *m_lDefect[i] = iniRead.value(QString("Defect_%1/parame%2").arg(i + 1).arg(j)).toInt();
            m_lDefect[i]++;
        }

        for (int j = 0; j < 14; j++){
            *m_lRectTS[i] = iniRead.value(QString("RectTS_%1/parame%2").arg(i + 1).arg(j)).toFloat();
            m_lRectTS[i]++;
        }


        for (int j = 0; j < 8; j++){
            *m_lRectTSS[i] = iniRead.value(QString("RectTSS_%1/parame%2").arg(i + 1).arg(j)).toFloat();
            m_lRectTSS[i]++;
        }

        for (int j = 0; j < 9; j++){
            *m_lSide[i] = iniRead.value(QString("Side_%1/parame%2").arg(i + 1).arg(j)).toFloat();
            m_lSide[i]++;
        }

        for (int j = 0; j < 4; j++){
            *m_lAlgPlace[i] = iniRead.value(QString("AlgPlace_%1/parame%2").arg(i + 1).arg(j)).toFloat();
            m_lAlgPlace[i]++;
        }

        path = iniRead.value(QString("ModelId_%1/path").arg(i + 1)).toString();
        try
        {
            if (!path.isEmpty()){
                HalconCpp::ReadShapeModel(path.toStdString().c_str(), &ModelID);
                //			HalconCpp::ReadDeformableModel(path.toStdString().c_str(), &ModelID);
                m_lAlg[i]->m_worker->ModelID = ModelID;
                m_lAlg[i]->m_worker->strPath = path;

            }
            m_lAlg[i]++;
        }
        catch (HException ex)
        {
            QMessageBox::information(this, tr("Error"), QString::fromLocal8Bit("读取mModelID 失败"));
        }

    }

    // 读取检测设置
    int* swAlg = &m_tAlg1->m_worker->m_swAlg.m_ccd1.nCCD;

    QString sInfo;
    for (int a = 0; a < 40; a++)
    {
        sInfo = QString("AlgSwitch/parame%1").arg(a);
        *swAlg = iniRead.value(sInfo).toInt();
        swAlg++;
    }
    m_tAlg2->m_worker->m_swAlg = m_tAlg1->m_worker->m_swAlg;
    m_tAlg3->m_worker->m_swAlg = m_tAlg1->m_worker->m_swAlg;
    m_tAlg4->m_worker->m_swAlg = m_tAlg1->m_worker->m_swAlg;
    m_tAlg5->m_worker->m_swAlg = m_tAlg1->m_worker->m_swAlg;
    m_tAlg6->m_worker->m_swAlg = m_tAlg1->m_worker->m_swAlg;
    m_tAlg7->m_worker->m_swAlg = m_tAlg1->m_worker->m_swAlg;
    m_tAlg8->m_worker->m_swAlg = m_tAlg1->m_worker->m_swAlg;

    setAlgOptions(m_tAlg1, m_tAlg1->m_worker->m_swAlg.m_ccd1);
    setAlgOptions(m_tAlg2, m_tAlg1->m_worker->m_swAlg.m_ccd2);
    setAlgOptions(m_tAlg3, m_tAlg1->m_worker->m_swAlg.m_ccd3);
    setAlgOptions(m_tAlg4, m_tAlg1->m_worker->m_swAlg.m_ccd4);
    setAlgOptions(m_tAlg5, m_tAlg1->m_worker->m_swAlg.m_ccd5);
    setAlgOptions(m_tAlg6, m_tAlg1->m_worker->m_swAlg.m_ccd6);
    setAlgOptions(m_tAlg7, m_tAlg1->m_worker->m_swAlg.m_ccd7);
    setAlgOptions(m_tAlg8, m_tAlg1->m_worker->m_swAlg.m_ccd8);

    updateAlgInfo();
}

void MainWindow::RdModeId()
{
    QString path = QDir::currentPath() + "/Template/142.shm";
    HTuple ModelID;
    HalconCpp::ReadShapeModel(path.toStdString().c_str(), &ModelID);
}



void MainWindow::SearchProFiles()
{

    // 获取当前文件所有Ini名称
    QString path = QDir::currentPath() + "/Config/Product";
    QDir dir(path);
    file_list = dir.entryList(QDir::Files | QDir::NoDotAndDotDot);

    if (file_list.size() == 1){
        m_dProduct->ui->deleteBt->setEnabled(false);
    }
    else{
        m_dProduct->ui->deleteBt->setEnabled(true);
    }
    SetCombox();
}

//删除文件夹
bool MainWindow::deleteDir(const QString &path)
{

    if (path.isEmpty()){
        return false;
    }
    QDir dir(path);
    if (!dir.exists()){
        return true;
    }
    dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot); //设置过滤
    QFileInfoList fileList = dir.entryInfoList(); // 获取所有的文件信息
    foreach(QFileInfo file, fileList){ //遍历文件信息
        if (file.isFile()){ // 是文件，删除
            file.dir().remove(file.fileName());
        }
        else{ // 递归删除
            deleteDir(file.absoluteFilePath());
        }
    }
    return true;
}

void MainWindow::on_Main_BtReset_clicked()
{
    // 数据重置
    ClearCameraData();

    // 数据瞬时保存
    SaveToIni();
}

void MainWindow::on_Main_BtProductManage_clicked()
{
    // 生产管理
    m_dProduct->show();
}

void MainWindow::ClearCameraData(){
    m_nDelet = 0;  // 删除图片数据
    m_nNG1 = 0;
    m_nNG2 = 0;
    m_nNG3 = 0;
    m_nNG4 = 0;
    m_nOK = 0;

    m_nRev = 0;  // 速率
    m_nTime = 0;
    m_nOldTime = 0;

    // 速率
    strRev = "0";

    // 运行时间

    m_nSS = 0;
    m_nMM = 0;
    m_nHH = 0;
    m_nDay = 0;

    m_nCameraResult = 0; // 运算结果总量
    m_nGod1 = 0;
    m_nGod2 = 0;
    m_nGod3 = 0;
    m_nGod4 = 0;
    m_nGod5 = 0;
    m_nGod6 = 0;
    m_nGod7 = 0;
    m_nGod8 = 0;

    m_nBad1 = 0;
    m_nBad2 = 0;
    m_nBad3 = 0;
    m_nBad4 = 0;
    m_nBad5 = 0;
    m_nBad6 = 0;
    m_nBad7 = 0;
    m_nBad8 = 0;

    m_sRunTime = "";

    ShowUI();
    SetRunTime();

    M_NUMBER m_nAlgNG;
    M_NUMBER m_nAlgOK;

    M_NUMBER m_nAlgUp;
    M_NUMBER m_nAlgDown;

    m_tAlg1->m_worker->m_nAlgNG = m_nAlgNG;
    m_tAlg2->m_worker->m_nAlgNG = m_nAlgNG;
    m_tAlg3->m_worker->m_nAlgNG = m_nAlgNG;
    m_tAlg4->m_worker->m_nAlgNG = m_nAlgNG;
    m_tAlg5->m_worker->m_nAlgNG = m_nAlgNG;
    m_tAlg6->m_worker->m_nAlgNG = m_nAlgNG;
    m_tAlg7->m_worker->m_nAlgNG = m_nAlgNG;
    m_tAlg8->m_worker->m_nAlgNG = m_nAlgNG;


    m_tAlg1->m_worker->m_nAlgOK = m_nAlgOK;
    m_tAlg2->m_worker->m_nAlgOK = m_nAlgOK;
    m_tAlg3->m_worker->m_nAlgOK = m_nAlgOK;
    m_tAlg4->m_worker->m_nAlgOK = m_nAlgOK;
    m_tAlg5->m_worker->m_nAlgOK = m_nAlgOK;
    m_tAlg6->m_worker->m_nAlgOK = m_nAlgOK;
    m_tAlg7->m_worker->m_nAlgOK = m_nAlgOK;
    m_tAlg8->m_worker->m_nAlgOK = m_nAlgOK;


    m_tAlg1->m_worker->m_nAlgUp = m_nAlgUp;
    m_tAlg2->m_worker->m_nAlgUp = m_nAlgUp;
    m_tAlg3->m_worker->m_nAlgUp = m_nAlgUp;
    m_tAlg4->m_worker->m_nAlgUp = m_nAlgUp;
    m_tAlg5->m_worker->m_nAlgUp = m_nAlgUp;
    m_tAlg6->m_worker->m_nAlgUp = m_nAlgUp;
    m_tAlg7->m_worker->m_nAlgUp = m_nAlgUp;
    m_tAlg8->m_worker->m_nAlgUp = m_nAlgUp;


    m_tAlg1->m_worker->m_nAlgDown = m_nAlgDown;
    m_tAlg2->m_worker->m_nAlgDown = m_nAlgDown;
    m_tAlg3->m_worker->m_nAlgDown = m_nAlgDown;
    m_tAlg4->m_worker->m_nAlgDown = m_nAlgDown;
    m_tAlg5->m_worker->m_nAlgDown = m_nAlgDown;
    m_tAlg6->m_worker->m_nAlgDown = m_nAlgDown;
    m_tAlg7->m_worker->m_nAlgDown = m_nAlgDown;
    m_tAlg8->m_worker->m_nAlgDown = m_nAlgDown;



    m_tAlg1->m_worker->m_nDefectAbn = 0;
    m_tAlg2->m_worker->m_nDefectAbn = 0;
    m_tAlg3->m_worker->m_nDefectAbn = 0;
    m_tAlg4->m_worker->m_nDefectAbn = 0;
    m_tAlg5->m_worker->m_nDefectAbn = 0;
    m_tAlg6->m_worker->m_nDefectAbn = 0;
    m_tAlg7->m_worker->m_nDefectAbn = 0;
    m_tAlg8->m_worker->m_nDefectAbn = 0;


    m_tAlg1->m_worker->m_nRectAbn = 0;
    m_tAlg2->m_worker->m_nRectAbn = 0;
    m_tAlg3->m_worker->m_nRectAbn = 0;
    m_tAlg4->m_worker->m_nRectAbn = 0;
    m_tAlg5->m_worker->m_nRectAbn = 0;
    m_tAlg6->m_worker->m_nRectAbn = 0;
    m_tAlg7->m_worker->m_nRectAbn = 0;
    m_tAlg8->m_worker->m_nRectAbn = 0;
}

void MainWindow::on_Main_BtLight_clicked()
{
    // 启动照明
    m_dPlc->on_inBT_25_clicked();
}

void MainWindow::on_Main_BtDiscA_clicked()
{
    // 启动震动盘
    m_dPlc->on_inBT_7_clicked();
}

void MainWindow::on_Main_BtMotorA_clicked()
{
    // 启动上料电机
}

void MainWindow::on_Main_BtDiscB_clicked()
{
    // 启动玻璃盘
    m_nMoto1 = 1 - m_nMoto1;
    m_dPlc->m_tPlc->m_worker->writePlcInfo(MOTOR1, m_nMoto1);
}

void MainWindow::on_Main_BtLogin_clicked()
{
    // 登录
    ui->Main_BtPLCManage->show();
    SetAlgHideUi(1);
}

void MainWindow::on_Main_BtLogout_clicked()
{
    // 锁定
    ui->Main_BtPLCManage->hide();
    SetAlgHideUi(0);
}

void MainWindow::on_CCD_singleBt_clicked()
{
    // 单张硬触发
    switch (m_nMainCurrent){
    case 1:
        m_dPlc->on_inBT_10_clicked();
        break;
    case 2:
        m_dPlc->on_inBT_11_clicked();
        break;
    case 3:
        m_dPlc->on_inBT_12_clicked();
        break;
    case 4:
        m_dPlc->on_inBT_13_clicked();
        break;
    case 5:
        m_dPlc->on_inBT_14_clicked();
        break;
    case 6:
        m_dPlc->on_inBT_15_clicked();
        break;
    case 7:
        m_dPlc->on_inBT_34_clicked();
        break;
    case 8:
        m_dPlc->on_inBT_35_clicked();
        break;
    default:
        break;
    }
}

void MainWindow::on_CCD_plateBt_clicked()
{
    // 启动玻璃盘
    on_Main_BtDiscB_clicked();
}

void MainWindow::on_CCD_multiBt_clicked()
{
    // 连续硬触发
    m_nContinuous = 1 - m_nContinuous;

}

void MainWindow::on_pushButton_9_clicked()
{
    // 导入CAD
}

void MainWindow::on_Main_ComboxProduct_currentIndexChanged(int index)
{
    // 选择产品
    if (index < 0){
        return;
    }
    if (!bProSave)
    {
        bProSave = true;
        ui->Main_ComboxProduct->setCurrentText(m_strName);
        return;
    }
    m_nProductNum = index;
    RdProductIni(file_list[index]);
}

void MainWindow::on_changeBt_clicked()
{
    if (!m_nMainCurrent){
        return;
    }
    QString iniFilePath = "Config/Product/" + file_list[m_nProductNum];
    QSettings configIniWrite(iniFilePath, QSettings::IniFormat);
    configIniWrite.setIniCodec(QTextCodec::codecForName("UTF_8"));
    QString sInfo;

    // 清除 参数UI
    int* nDefect;
    float* fDSector;
    float* fDSide;
    int* nDFback;


    float* fRect;
    float* fSector;
    float* fSide;

    float* fRectTS;
    float* fRectTSS;
    float* fTrapezoid;
    float* fBullet;



    AlgThread* m_tAlg;
    switch (m_nMainCurrent)
    {
    case 1:
        m_tAlg = m_tAlg1;
        break;
    case 2:
        m_tAlg = m_tAlg2;
        break;
    case 3:
        m_tAlg = m_tAlg3;
        break;
    case 4:
        m_tAlg = m_tAlg4;
        break;
    case 5:
        m_tAlg = m_tAlg5;
        break;
    case 6:
        m_tAlg = m_tAlg6;
        break;
    case 7:
        m_tAlg = m_tAlg7;
        break;
    case 8:
        m_tAlg = m_tAlg8;
        break;
    default:
        return;
        break;
    }

    nDFback = &m_tAlg->m_worker->m_defectFback.m_part.para1;
    nDefect = &m_tAlg->m_worker->m_defectRect.m_part.para1;
    fRectTS = &m_tAlg->m_worker->m_measureTS.m_part.para1;
    fRectTSS = &m_tAlg->m_worker->m_measureTSS.m_part.para1;
    fRect = &m_tAlg->m_worker->m_measureRect.m_part.para1;
    fSide = &m_tAlg->m_worker->m_measureSide.m_part.para1;
    fTrapezoid = &m_tAlg->m_worker->m_measureTrapezoid.m_part.para1;
    fBullet = &m_tAlg->m_worker->m_measureBullet.m_part.para1;


    fSector = &m_tAlg->m_worker->m_measureSector.m_part.para1;
    fDSector = &m_tAlg->m_worker->m_defectSector.m_part.para1;
    fDSide = &m_tAlg->m_worker->m_defectSide.m_part.para1;

    QLineEdit* lE;
    for (int i = 0; i < 10; i++){
        lE = this->findChild<QLineEdit*>("ccd_DFback_" + QString::number(i + 1));
        *nDFback = lE->text().toInt();
        sInfo = QString("DFback_%1/parame%2").arg(m_nMainCurrent).arg(i);
        configIniWrite.setValue(sInfo, lE->text());
        nDFback++;
    }



    for (int i = 0; i < 8; i++){
        lE = this->findChild<QLineEdit*>("ccd_DSector_" + QString::number(i + 1));
        *fDSector = lE->text().toFloat();
        sInfo = QString("DSector_%1/parame%2").arg(m_nMainCurrent).arg(i);
        configIniWrite.setValue(sInfo, lE->text());
        fDSector++;
    }

    for (int i = 0; i < 12; i++){
        lE = this->findChild<QLineEdit*>("ccd_DSide_" + QString::number(i + 1));
        *fDSide = lE->text().toFloat();
        sInfo = QString("DSide_%1/parame%2").arg(m_nMainCurrent).arg(i);
        configIniWrite.setValue(sInfo, lE->text());
        fDSide++;
    }


    for (int i = 0; i < 30; i++){
        lE = this->findChild<QLineEdit*>("ccd_defect_" + QString::number(i + 1));
        *nDefect = lE->text().toInt();
        sInfo = QString("Defect_%1/parame%2").arg(m_nMainCurrent).arg(i);
        configIniWrite.setValue(sInfo, lE->text());
        nDefect++;
    }

    for (int i = 0; i < 24; i++)
    {
        lE = this->findChild<QLineEdit*>("ccd_sector_" + QString::number(i + 1));
        *fSector = lE->text().toFloat();
        sInfo = QString("Sector_%1/parame%2").arg(m_nMainCurrent).arg(i);
        configIniWrite.setValue(sInfo, lE->text());
        fSector++;
    }

    for (int i = 0; i < 19; i++)
    {
        lE = this->findChild<QLineEdit*>("ccd_trapezoid_" + QString::number(i + 1));
        *fTrapezoid = lE->text().toFloat();
        sInfo = QString("Trapezoid_%1/parame%2").arg(m_nMainCurrent).arg(i);
        configIniWrite.setValue(sInfo, lE->text());
        fTrapezoid++;
    }

    for (int i = 0; i < 36; i++)
    {
        lE = this->findChild<QLineEdit*>("ccd_rect_" + QString::number(i + 1));
        *fRect = lE->text().toFloat();
        sInfo = QString("Rect_%1/parame%2").arg(m_nMainCurrent).arg(i);
        configIniWrite.setValue(sInfo, lE->text());
        fRect++;
    }


    for (int i = 0; i < 36; i++)
    {
        lE = this->findChild<QLineEdit*>("ccd_rect_" + QString::number(i + 1));
        *fBullet = lE->text().toFloat();
        sInfo = QString("Bullet_%1/parame%2").arg(m_nMainCurrent).arg(i);
        configIniWrite.setValue(sInfo, lE->text());
        fBullet++;
    }


    for (int i = 0; i < 9; i++)
    {
        lE = this->findChild<QLineEdit*>("ccd_side_" + QString::number(i + 1));
        *fSide = lE->text().toFloat();
        sInfo = QString("Side_%1/parame%2").arg(m_nMainCurrent).arg(i);
        configIniWrite.setValue(sInfo, lE->text());
        fSide++;
    }


    for (int i = 0; i < 14; i++)
    {
        lE = this->findChild<QLineEdit*>("ccd_rectTS_" + QString::number(i + 1));
        *fRectTS = lE->text().toFloat();
        sInfo = QString("RectTS_%1/parame%2").arg(m_nMainCurrent).arg(i);
        configIniWrite.setValue(sInfo, lE->text());
        fRectTS++;
    }


    for (int i = 0; i < 8; i++)
    {
        lE = this->findChild<QLineEdit*>("ccd_rectTSS_" + QString::number(i + 1));
        *fRectTSS = lE->text().toFloat();
        sInfo = QString("RectTSS_%1/parame%2").arg(m_nMainCurrent).arg(i);
        configIniWrite.setValue(sInfo, lE->text());
        fRectTSS++;
    }
    InitializeAlgData();
    QMessageBox::information(this, tr("Success"), QString::fromLocal8Bit("写入成功！"));
}

void MainWindow::on_ClearBt_clicked()
{
    //    清除图片
    if (QMessageBox::information(this, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("以下操作会清除所有图片！"), tr("Yes"), tr("No")))
    {
        return;
    }
    QString imgPath = QDir::currentPath() + "/Pic";
    deleteDir(imgPath);
    QMessageBox::information(this, QString::fromLocal8Bit("成功"), QString::fromLocal8Bit("所有图片已经清除完成！"));
}
void MainWindow::updateAlgInfo()
{
    // 更新检测值
    if (!m_nMainCurrent){
        return;
    }

    m_nRectType = 0;
    m_nDefctType = 0;

    // 清除 参数UI
    ui->tab_Alg->clear();
    ui->scrollArea_2->hide();
    SW_AlgPart m_ccd;
    float* fRect;
    float* fSector;
    float* fSide;
    float* fTrapezoid;
    int* nDefect;
    float* fRectTS;
    float* fRectTSS;
    float* fDSector;
    float* fBullet;

    float* fDSide;
    int* nDFback;

    AlgThread* m_tAlg;
    switch (m_nMainCurrent)
    {
    case 1:
        m_ccd = m_tAlg1->m_worker->m_swAlg.m_ccd1;
        m_tAlg = m_tAlg1;
        break;
    case 2:
        m_ccd = m_tAlg2->m_worker->m_swAlg.m_ccd2;
        m_tAlg = m_tAlg2;
        break;
    case 3:
        m_ccd = m_tAlg3->m_worker->m_swAlg.m_ccd3;
        m_tAlg = m_tAlg3;
        break;
    case 4:
        m_ccd = m_tAlg4->m_worker->m_swAlg.m_ccd4;
        m_tAlg = m_tAlg4;
        break;
    case 5:
        m_ccd = m_tAlg5->m_worker->m_swAlg.m_ccd5;
        m_tAlg = m_tAlg5;
        break;
    case 6:
        m_ccd = m_tAlg6->m_worker->m_swAlg.m_ccd6;
        m_tAlg = m_tAlg6;
        break;
    case 7:
        m_ccd = m_tAlg7->m_worker->m_swAlg.m_ccd7;
        m_tAlg = m_tAlg7;
        break;
    case 8:
        m_ccd = m_tAlg8->m_worker->m_swAlg.m_ccd8;
        m_tAlg = m_tAlg8;
        break;
    default:
        return;
        break;
    }

    nDFback = &m_tAlg->m_worker->m_defectFback.m_part.para1;

    fRect = &m_tAlg->m_worker->m_measureRect.m_part.para1;
    fSide = &m_tAlg->m_worker->m_measureSide.m_part.para1;
    fRectTS = &m_tAlg->m_worker->m_measureTS.m_part.para1;
    fRectTSS = &m_tAlg->m_worker->m_measureTSS.m_part.para1;
    fTrapezoid = &m_tAlg->m_worker->m_measureTrapezoid.m_part.para1;
    nDefect = &m_tAlg->m_worker->m_defectRect.m_part.para1;
    fSector = &m_tAlg->m_worker->m_measureSector.m_part.para1;

    fDSector = &m_tAlg->m_worker->m_defectSector.m_part.para1;
    fDSide = &m_tAlg->m_worker->m_defectSide.m_part.para1;



    fBullet = &m_tAlg->m_worker->m_measureBullet.m_part.para1;

    // 显示 alg palce
    ui->CEdit_check1->setText(QString::number(m_tAlg->m_worker->m_algPlace.x1.D()));
    ui->CEdit_check2->setText(QString::number(m_tAlg->m_worker->m_algPlace.y1.D()));
    ui->CEdit_check3->setText(QString::number(m_tAlg->m_worker->m_algPlace.x2.D()));
    ui->CEdit_check4->setText(QString::number(m_tAlg->m_worker->m_algPlace.y2.D()));


    // 显示模板
    ui->CCD_Edit6->setText(m_tAlg->m_worker->strPath);


    QLineEdit* lE;

    if (m_ccd.nCCD)
    {
        if (m_ccd.nAlgA)
        {
            m_nDefctType = m_ccd.nDefect;
            switch (m_ccd.nDefect)
            {
            case 1:
                // 添加缺陷1参数界面
                for (int i = 0; i < 30; i++){
                    lE = this->findChild<QLineEdit*>("ccd_defect_" + QString::number(i + 1));
                    lE->setText(QString::number(*nDefect));
                    nDefect++;
                }

                ui->tab_Alg->insertTab(0, m_tabA, QString::fromLocal8Bit("缺陷:方形"));
                break;
            case 2:
                // 添加缺陷2参数界面
                for (int i = 0; i < 8; i++)
                {
                    lE = this->findChild<QLineEdit*>("ccd_DSector_" + QString::number(i + 1));
                    lE->setText(QString::number(*fDSector));
                    fDSector++;
                }
                ui->tab_Alg->insertTab(7, m_tabH, QString::fromLocal8Bit("缺陷:扇形"));
                break;
            case 3:
                // 添加缺陷3参数界面
                for (int i = 0; i < 12; i++)
                {
                    lE = this->findChild<QLineEdit*>("ccd_DSide_" + QString::number(i + 1));
                    lE->setText(QString::number(*fDSide));
                    fDSide++;
                }


                ui->tab_Alg->insertTab(8, m_tabI, QString::fromLocal8Bit("缺陷:侧面"));
                break;
            case 4:
                // 添加缺陷4参数界面
                for (int i = 0; i < 10; i++)
                {
                    lE = this->findChild<QLineEdit*>("ccd_DFback_" + QString::number(i + 1));
                    lE->setText(QString::number(*nDFback));
                    nDFback++;
                }
                ui->tab_Alg->insertTab(0, m_tabJ, QString::fromLocal8Bit("缺陷:背面"));
                break;
            }
            ui->scrollArea_2->show();
        }
        if (m_ccd.nAlgB)
        {
            m_nRectType = m_ccd.nRect;
            switch (m_ccd.nRect)
            {
            case 1:
                // 添加尺寸1参数界面
                for (int i = 0; i < 36; i++){
                    lE = this->findChild<QLineEdit*>("ccd_rect_" + QString::number(i + 1));
                    lE->setText(QString::number(*fRect));
                    fRect++;
                }
                ui->tab_Alg->insertTab(1, m_tabB, QString::fromLocal8Bit("正长方形(全检)"));
                break;
            case 2:
                // 添加尺寸2参数界面
                for (int i = 0; i < 19; i++){
                    lE = this->findChild<QLineEdit*>("ccd_trapezoid_" + QString::number(i + 1));
                    lE->setText(QString::number(*fTrapezoid));
                    fTrapezoid++;
                }

                ui->tab_Alg->insertTab(2, m_tabC, QString::fromLocal8Bit("梯形参数"));
                break;
            case 3:
                // 添加尺寸3参数界面


                for (int i = 0; i < 9; i++){
                    lE = this->findChild<QLineEdit*>("ccd_side_" + QString::number(i + 1));
                    lE->setText(QString::number(*fSide));
                    fSide++;
                }

                ui->tab_Alg->insertTab(5, m_tabF, QString::fromLocal8Bit("正长方形(厚度)"));
                break;
            case 4:
                // 添加尺寸4参数界面
                for (int i = 0; i < 14; i++){
                    lE = this->findChild<QLineEdit*>("ccd_rectTS_" + QString::number(i + 1));
                    lE->setText(QString::number(*fRectTS));
                    fRectTS++;
                }
                ui->tab_Alg->insertTab(3, m_tabD, QString::fromLocal8Bit("异形正面参数"));
                break;
            case 5:
                // 添加尺寸5参数界面
                for (int i = 0; i < 8; i++){
                    lE = this->findChild<QLineEdit*>("ccd_rectTSS_" + QString::number(i + 1));
                    lE->setText(QString::number(*fRectTSS));
                    fRectTSS++;
                }
                ui->tab_Alg->insertTab(4, m_tabE, QString::fromLocal8Bit("异形侧面参数"));
                break;
            case 7:
                // 添加尺寸5参数界面
                for (int i = 0; i < 24; i++)
                {
                    lE = this->findChild<QLineEdit*>("ccd_sector_" + QString::number(i + 1));
                    lE->setText(QString::number(*fSector));
                    fSector++;
                }
                ui->tab_Alg->insertTab(6, m_tabG, QString::fromLocal8Bit("尺寸检测：扇形"));
                break;
            case 8:
                // 尺寸 子弹头检测

                for (int i = 0; i < 36; i++){
                    lE = this->findChild<QLineEdit*>("ccd_rect_" + QString::number(i + 1));
                    lE->setText(QString::number(*fBullet));
                    fBullet++;
                }
                ui->tab_Alg->insertTab(1, m_tabB, QString::fromLocal8Bit("尺寸检测：子弹头"));
                break;
            }
            ui->scrollArea_2->show();
        }
    }
    InitializeAlgData();

}

void MainWindow::setAlgOptions(AlgThread* tAlg, SW_AlgPart ccd)
{
    if (ccd.nCCD){
        if (ccd.nAlgA){
            tAlg->m_worker->m_nAlgDefect = ccd.nDefect;
        }
        else{
            tAlg->m_worker->m_nAlgDefect = 0;
        }

        if (ccd.nAlgB){
            tAlg->m_worker->m_nAlgRect = ccd.nRect;
        }
        else{
            tAlg->m_worker->m_nAlgRect = 0;
        }
    }
    else{
        tAlg->m_worker->m_nAlgDefect = 0;
        tAlg->m_worker->m_nAlgRect = 0;
    }
}

void MainWindow::mousePressEvent(QMouseEvent *event)
{

    QPoint s(ui->centralWidget->pos() + ui->CCD1_Wind->pos());//控件在窗口内的坐标
    lastPoint = QPoint(event->pos().x() - s.x(), event->pos().y() - s.y());//鼠标在控件上的坐标

    mouseDown = true;
    if (ui->label->rect().contains(lastPoint))
    {
        firstPoint = lastPoint;
        m_dXO = firstPoint.x();
        m_dYO = firstPoint.y();
    }

}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    QPoint s(ui->centralWidget->pos() + ui->CCD1_Wind->pos());//控件在窗口内的坐标
    lastPoint = QPoint(event->pos().x() - s.x(), event->pos().y() - s.y());//鼠标在控件上的坐标
    if (ui->CCD1_Wind->rect().contains(lastPoint))
    {
        windowPoint = lastPoint;
    }
    else
    {
        windowPoint = QPoint(-1, -1);
    }
}

void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    //moveWnd(windowPoint, m_hImage, hv_WindowHandle);
    //mouseDown = false;
}

void MainWindow::wheelEvent(QWheelEvent *event)
{
    //short zDelta = short(event->delta());
    //if (zDelta > 0)           //图像放大
    //{
    //    if (zoom_scale < 6)      //最大放大6倍
    //    {
    //        zoom_scale = zoom_scale * 1.05;
    //        displayImage(m_hImage, hv_WindowHandle);
    //    }
    //}
    //else if (zDelta < 0)                   //图像缩小
    //{
    //    if (zoom_scale > 1)
    //    {
    //        zoom_scale = zoom_scale / 1.05;
    //        if (zoom_scale < 1)
    //        {
    //            zoom_scale = 1;
    //        }
    //        displayImage(m_hImage, hv_WindowHandle);
    //    }
    //}
}

void MainWindow::displayImage(HImage srcImg, HTuple hv_Window)
{
    double dWidth = srcImg.Width().I();
    double dHeight = srcImg.Height().I();
    double dWidth2 = dWidth / zoom_scale;
    double dHeight2 = dHeight / zoom_scale;
    if (dHeight / 2 - dHeight2 / 2 >= m_dYOffset && dHeight / 2 + dHeight2 / 2 - m_dYOffset <= dHeight)
    {
        m_dDispImagePartRow0 = dHeight / 2 - dHeight2 / 2 - m_dYOffset;
        m_dDispImagePartRow1 = dHeight / 2 + dHeight2 / 2 - m_dYOffset;
    }
    else if (dHeight / 2 - dHeight2 / 2 <= m_dYOffset)
    {
        m_dYOffset = dHeight / 2 - dHeight2 / 2;
        m_dDispImagePartRow0 = dHeight / 2 - dHeight2 / 2 - m_dYOffset;
        m_dDispImagePartRow1 = dHeight / 2 + dHeight2 / 2 - m_dYOffset;
    }
    else if (dHeight / 2 + dHeight2 / 2 - m_dYOffset >= dHeight)
    {
        m_dYOffset = dHeight / 2 + dHeight2 / 2 - dHeight;
        m_dDispImagePartRow0 = dHeight / 2 - dHeight2 / 2 - m_dYOffset;
        m_dDispImagePartRow1 = dHeight / 2 + dHeight2 / 2 - m_dYOffset;
    }
    if (dWidth / 2 - dWidth2 / 2 >= m_dXOffset && dWidth / 2 + dWidth2 / 2 - m_dXOffset <= dWidth)
    {
        m_dDispImagePartCol0 = dWidth / 2 - dWidth2 / 2 - m_dXOffset;
        m_dDispImagePartCol1 = dWidth / 2 + dWidth2 / 2 - m_dXOffset;
    }
    else if (dWidth / 2 - dWidth2 / 2 <= m_dXOffset)
    {
        m_dXOffset = dWidth / 2 - dWidth2 / 2;
        m_dDispImagePartCol0 = dWidth / 2 - dWidth2 / 2 - m_dXOffset;
        m_dDispImagePartCol1 = dWidth / 2 + dWidth2 / 2 - m_dXOffset;
    }
    else if (dWidth / 2 + dWidth2 / 2 - m_dXOffset >= dWidth)
    {
        m_dXOffset = dWidth / 2 + dWidth2 / 2 - dWidth;
        m_dDispImagePartCol0 = dWidth / 2 - dWidth2 / 2 - m_dXOffset;
        m_dDispImagePartCol1 = dWidth / 2 + dWidth2 / 2 - m_dXOffset;
    }
    SetPart(hv_Window, m_dDispImagePartRow0, m_dDispImagePartCol0, m_dDispImagePartRow1 - 1, m_dDispImagePartCol1 - 1);
    DispObj(srcImg, hv_Window);
}

void MainWindow::moveWnd(QPoint point, HImage srcImg, HTuple hWindow)
{
    QRect m_rPic = ui->CCD1_Wind->rect();
    double wWidth = m_rPic.right() - m_rPic.left();
    double wHeight = m_rPic.bottom() - m_rPic.top();
    double dWidth = srcImg.Width().I();
    double dHeight = srcImg.Height().I();
    int xOffset = point.x() - m_dXO;
    int yOffset = point.y() - m_dYO;
    m_dXOffset = m_dXOffset + (point.x() - m_dXO)*dWidth / wWidth / zoom_scale;
    m_dYOffset = m_dYOffset + (point.y() - m_dYO)*dHeight / wHeight / zoom_scale;
    if (m_dDispImagePartRow0 >= yOffset * dHeight / wHeight / zoom_scale && m_dDispImagePartRow1 - yOffset * dHeight / wHeight / zoom_scale <= dHeight)
    {
        m_dDispImagePartRow0 = m_dDispImagePartRow0 - yOffset * dHeight / wHeight / zoom_scale;
        m_dDispImagePartRow1 = m_dDispImagePartRow1 - yOffset * dHeight / wHeight / zoom_scale;
    }
    else if (m_dDispImagePartRow0 <= yOffset * dHeight / wHeight / zoom_scale)
    {
        m_dDispImagePartRow1 = m_dDispImagePartRow1 - m_dDispImagePartRow0;
        m_dDispImagePartRow0 = m_dDispImagePartRow0 - m_dDispImagePartRow0;
    }
    else if (m_dDispImagePartRow1 - yOffset * dHeight / wHeight / zoom_scale >= dHeight)
    {
        m_dDispImagePartRow0 = m_dDispImagePartRow0 - m_dDispImagePartRow1 + dHeight;
        m_dDispImagePartRow1 = m_dDispImagePartRow1 - m_dDispImagePartRow1 + dHeight;
    }
    if (m_dDispImagePartCol0 >= xOffset * dWidth / wWidth / zoom_scale && m_dDispImagePartCol1 - xOffset * dWidth / wWidth / zoom_scale <= dWidth)
    {
        m_dDispImagePartCol0 = m_dDispImagePartCol0 - xOffset * dWidth / wWidth / zoom_scale;
        m_dDispImagePartCol1 = m_dDispImagePartCol1 - xOffset * dWidth / wWidth / zoom_scale;
    }
    else if (m_dDispImagePartCol0 <= xOffset * dWidth / wWidth / zoom_scale)
    {
        m_dDispImagePartCol1 = m_dDispImagePartCol1 - m_dDispImagePartCol0;
        m_dDispImagePartCol0 = m_dDispImagePartCol0 - m_dDispImagePartCol0;
    }
    else if (m_dDispImagePartCol1 - xOffset * dWidth / wWidth / zoom_scale >= dWidth)
    {
        m_dDispImagePartCol0 = m_dDispImagePartCol0 - m_dDispImagePartCol1 + dWidth;
        m_dDispImagePartCol1 = m_dDispImagePartCol1 - m_dDispImagePartCol1 + dWidth;
    }
    ClearWindow(hWindow);
    SetPart(hWindow, m_dDispImagePartRow0, m_dDispImagePartCol0, m_dDispImagePartRow1 - 1, m_dDispImagePartCol1 - 1);
    DispObj(srcImg, hWindow);
}


void MainWindow::on_CCD_placeBt_clicked()
{
    QString path;
    HTuple Row1, Column1, Row2, Column2;
    HTuple ModelRow, ModelColumn, ModelAngle, ModelScore, ModelId, HomMat2D;
    HObject Roi, Contour, ContourAffine, TemplateImage, Img, HomMat2DIdentity;
    //HalconCpp::HTuple ModelID;

    try
    {
        if (!m_nMainCurrent > 0){
            return;
        }
        if (!isOpenImg){
            return;
        }
        path = QString("%1/Template/%2_%3.shm").arg(QDir::currentPath()).arg(m_strName).arg(m_nMainCurrent);

        HalconCpp::SetColor(m_hvHandle, "red");

        DrawRectangle1(m_hvHandle, &Row1, &Column1, &Row2, &Column2);
        GenRectangle1(&Roi, Row1, Column1, Row2, Column2);
        ReduceDomain(m_hImage, Roi, &TemplateImage);

        //		CreatePlanarUncalibDeformableModel(TemplateImage, 6, HTuple(0).TupleRad(), HTuple(360).TupleRad(),
        //			HTuple(1).TupleRad(), 1, 1, 0.02, 1, 1, 0.02, "none", "use_polarity", (HTuple(54).Append(85)),
        //			10, HTuple(), HTuple(), &ModelId);
        //		GetDeformableModelContours(&Contour, ModelId, 1);



        //        FindPlanarUncalibDeformableModel(Img, ModelId, HTuple(0).TupleRad(), HTuple(360).TupleRad(),
        //			1, 1, 1, 1, 0.5, 0, 0.5, 7, 0.75, "subpixel", "least_squares_very_high", &HomMat2d,
        //			&ModelScore);
        //		ProjectiveTransContourXld(Contour, &ContourAffine, HomMat2d);
        CreateShapeModel(TemplateImage, 6, HTuple(0).TupleRad(), HTuple(360).TupleRad(), HTuple(0.5379).TupleRad(), (HTuple("point_reduction_high").Append("no_pregeneration")), "use_polarity", HTuple(44).Append(63).Append(5), 4, &ModelId);
        GetShapeModelContours(&Contour, ModelId, 1);
        FindShapeModel(m_hImage, ModelId, HTuple(0).TupleRad(), HTuple(360).TupleRad(), 0.4, 0, 0.5, "least_squares", HTuple(6).Append(1), 0.75, &ModelRow, &ModelColumn, &ModelAngle, &ModelScore);

        VectorAngleToRigid(0, 0, 0, ModelRow, ModelColumn, ModelAngle, &HomMat2D);
        AffineTransContourXld(Contour, &ContourAffine, HomMat2D);

        DispObj(ContourAffine, m_hvHandle);
        if (QMessageBox::information(this, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("以下操作会创建新的模板匹配ID"), tr("Yes"), tr("No")))
        {
            return;
        }
        //		WriteDeformableModel(ModelId, path.toStdString().c_str());
        HalconCpp::WriteShapeModel(ModelId, path.toStdString().c_str());

        ui->CCD_Edit6->setText(path);

        // 存储模板数据地址
        QString iniFilePath = "Config/Product/" + file_list[m_nProductNum];
        QSettings configIniWrite(iniFilePath, QSettings::IniFormat);
        configIniWrite.setIniCodec(QTextCodec::codecForName("UTF_8"));
        QString sInfo;



        HalconCpp::ReadShapeModel(path.toStdString().c_str(), &ModelId);
        //		HalconCpp::ReadDeformableModel(path.toStdString().c_str(), &ModelId);
        m_tCurrentAlg->m_worker->ModelID = ModelId;
        m_tCurrentAlg->m_worker->strPath = path;

        sInfo = QString("ModelId_%1/path").arg(m_nMainCurrent);
        configIniWrite.setValue(sInfo, path);





        QMessageBox::information(this, tr("Success"), QString::fromLocal8Bit("创建成功！"));
    }
    catch (HException ex)
    {
        QMessageBox::information(this, tr("Success"), QString::fromLocal8Bit("创建失败！"));
    }


}


void MainWindow::on_CSetAlgPlace_clicked()
{
    if (!m_nMainCurrent){
        return;
    }
    QString iniFilePath = "Config/Product/" + file_list[m_nProductNum];
    QSettings configIniWrite(iniFilePath, QSettings::IniFormat);
    configIniWrite.setIniCodec(QTextCodec::codecForName("UTF_8"));
    QString sInfo;


    AlgThread* m_tAlg;
    HalconCpp::HTuple hvCCDHandle;
    switch (m_nMainCurrent) {
    case 1:
        m_tAlg = m_tAlg1;
        hvCCDHandle = c1_hvCCDHandle;
        break;
    case 2:
        m_tAlg = m_tAlg2;
        hvCCDHandle = c2_hvCCDHandle;
        break;
    case 3:
        m_tAlg = m_tAlg3;
        hvCCDHandle = c3_hvCCDHandle;
        break;
    case 4:
        m_tAlg = m_tAlg4;
        hvCCDHandle = c4_hvCCDHandle;
        break;
    case 5:
        m_tAlg = m_tAlg5;
        hvCCDHandle = c5_hvCCDHandle;
        break;
    case 6:
        m_tAlg = m_tAlg6;
        hvCCDHandle = c6_hvCCDHandle;
        break;
    case 7:
        m_tAlg = m_tAlg7;
        hvCCDHandle = c7_hvCCDHandle;
        break;
    case 8:
        m_tAlg = m_tAlg8;
        hvCCDHandle = c8_hvCCDHandle;
        break;
    default:
        return;
        break;
    }

    HalconCpp::HObject Rect, Contour;
    try
    {
        HalconCpp::ClearWindow(hvCCDHandle);
        HalconCpp::DispObj(m_hImage, hvCCDHandle);
        HalconCpp::SetColor(hvCCDHandle, "red");

        HalconCpp::DrawRectangle1(hvCCDHandle, &m_tAlg->m_worker->m_algPlace.x1, &m_tAlg->m_worker->m_algPlace.y1, &m_tAlg->m_worker->m_algPlace.x2, &m_tAlg->m_worker->m_algPlace.y2);
        HalconCpp::GenRectangle1(&Rect, m_tAlg->m_worker->m_algPlace.x1, m_tAlg->m_worker->m_algPlace.y1, m_tAlg->m_worker->m_algPlace.x2, m_tAlg->m_worker->m_algPlace.y2);
        HalconCpp::GenContourRegionXld(Rect, &Contour, "border");
        HalconCpp::SetColor(hvCCDHandle, "red");
        HalconCpp::DispObj(Contour, hvCCDHandle);

        HalconCpp::HTuple* m_value = &m_tAlg->m_worker->m_algPlace.x1;
        ui->CEdit_check1->setText(QString::number(m_tAlg->m_worker->m_algPlace.x1.D()));
        ui->CEdit_check2->setText(QString::number(m_tAlg->m_worker->m_algPlace.y1.D()));
        ui->CEdit_check3->setText(QString::number(m_tAlg->m_worker->m_algPlace.x2.D()));
        ui->CEdit_check4->setText(QString::number(m_tAlg->m_worker->m_algPlace.y2.D()));

        for (int i = 0; i < 4; i++)
        {
            sInfo = QString("AlgPlace_%1/parame%2").arg(m_nMainCurrent).arg(i);
            configIniWrite.setValue(sInfo, m_value->D());
            m_value++;
        }
    }
    catch (HalconCpp::HException ex)
    {

    }

}


void MainWindow::on_CCgeAlgPlace_clicked()
{
    if (!m_nMainCurrent){
        return;
    }
    QString iniFilePath = "Config/Product/" + file_list[m_nProductNum];
    QSettings configIniWrite(iniFilePath, QSettings::IniFormat);
    configIniWrite.setIniCodec(QTextCodec::codecForName("UTF_8"));
    QString sInfo;


    AlgThread* m_tAlg;
    HalconCpp::HTuple hvCCDHandle;
    switch (m_nMainCurrent) {
    case 1:
        m_tAlg = m_tAlg1;
        hvCCDHandle = c1_hvCCDHandle;
        break;
    case 2:
        m_tAlg = m_tAlg2;
        hvCCDHandle = c2_hvCCDHandle;
        break;
    case 3:
        m_tAlg = m_tAlg3;
        hvCCDHandle = c3_hvCCDHandle;
        break;
    case 4:
        m_tAlg = m_tAlg4;
        hvCCDHandle = c4_hvCCDHandle;
        break;
    case 5:
        m_tAlg = m_tAlg5;
        hvCCDHandle = c5_hvCCDHandle;
        break;
    case 6:
        m_tAlg = m_tAlg6;
        hvCCDHandle = c6_hvCCDHandle;
        break;
    case 7:
        m_tAlg = m_tAlg7;
        hvCCDHandle = c7_hvCCDHandle;
        break;
    case 8:
        m_tAlg = m_tAlg8;
        hvCCDHandle = c8_hvCCDHandle;
        break;
    default:
        return;
        break;
    }

    HalconCpp::HTuple* m_value = &m_tAlg->m_worker->m_algPlace.x1;
    QLineEdit* lE;

    for (int i = 0; i < 4; i++)
    {
        lE = this->findChild<QLineEdit*>("CEdit_check" + QString::number(i + 1));
        sInfo = QString("AlgPlace_%1/parame%2").arg(m_nMainCurrent).arg(i);
        configIniWrite.setValue(sInfo, lE->text().toDouble());
        *m_value = lE->text().toDouble();
        m_value++;
    }
    QMessageBox::information(this, tr("Success"), QString::fromLocal8Bit("修改成功！"));
}

void MainWindow::SetStopParam()
{
    m_tAlg1->m_worker->m_nModel = 1;
    m_tAlg2->m_worker->m_nModel = 1;
    m_tAlg3->m_worker->m_nModel = 1;
    m_tAlg4->m_worker->m_nModel = 1;
    m_tAlg5->m_worker->m_nModel = 1;
    m_tAlg6->m_worker->m_nModel = 1;
    m_tAlg7->m_worker->m_nModel = 1;
    m_tAlg8->m_worker->m_nModel = 1;
}

void MainWindow::SetStartParam()
{
    m_tAlg1->m_worker->m_nResult = 0;
    m_tAlg2->m_worker->m_nResult = 0;
    m_tAlg3->m_worker->m_nResult = 0;
    m_tAlg4->m_worker->m_nResult = 0;
    m_tAlg5->m_worker->m_nResult = 0;
    m_tAlg6->m_worker->m_nResult = 0;
    m_tAlg7->m_worker->m_nResult = 0;
    m_tAlg8->m_worker->m_nResult = 0;


    m_tAlg1->m_worker->m_nModel = 0;
    m_tAlg2->m_worker->m_nModel = 0;
    m_tAlg3->m_worker->m_nModel = 0;
    m_tAlg4->m_worker->m_nModel = 0;
    m_tAlg5->m_worker->m_nModel = 0;
    m_tAlg6->m_worker->m_nModel = 0;
    m_tAlg7->m_worker->m_nModel = 0;
    m_tAlg8->m_worker->m_nModel = 0;
}

void MainWindow::SaveToIni()
{
    QString manufacturePath = "Config/manufacture.ini";
    QSettings manufactureIniRead(manufacturePath, QSettings::IniFormat);
    manufactureIniRead.setIniCodec(QTextCodec::codecForName("UTF_8"));

    manufactureIniRead.setValue("ProductNum/Num", m_nProductNum);

    manufactureIniRead.setValue("CM/m_nCameraResult", m_nCameraResult);
    manufactureIniRead.setValue("CM/m_nGod1", m_nGod1);
    manufactureIniRead.setValue("CM/m_nGod2", m_nGod2);
    manufactureIniRead.setValue("CM/m_nGod3", m_nGod3);
    manufactureIniRead.setValue("CM/m_nGod4", m_nGod4);
    manufactureIniRead.setValue("CM/m_nGod5", m_nGod5);
    manufactureIniRead.setValue("CM/m_nGod6", m_nGod6);
    manufactureIniRead.setValue("CM/m_nGod7", m_nGod7);
    manufactureIniRead.setValue("CM/m_nGod8", m_nGod8);


    manufactureIniRead.setValue("CM/m_nBad1", m_nBad1);
    manufactureIniRead.setValue("CM/m_nBad2", m_nBad2);
    manufactureIniRead.setValue("CM/m_nBad3", m_nBad3);
    manufactureIniRead.setValue("CM/m_nBad4", m_nBad4);
    manufactureIniRead.setValue("CM/m_nBad5", m_nBad5);
    manufactureIniRead.setValue("CM/m_nBad6", m_nBad6);
    manufactureIniRead.setValue("CM/m_nBad7", m_nBad7);
    manufactureIniRead.setValue("CM/m_nBad8", m_nBad8);

    manufactureIniRead.setValue("SUM/m_nNG1", m_nNG1);
    manufactureIniRead.setValue("SUM/m_nNG2", m_nNG2);
    manufactureIniRead.setValue("SUM/m_nNG3", m_nNG3);
    manufactureIniRead.setValue("SUM/m_nNG4", m_nNG4);
    manufactureIniRead.setValue("SUM/m_nOK", m_nOK);

    manufactureIniRead.setValue("TIME/m_nSS", m_nSS);
    manufactureIniRead.setValue("TIME/m_nMM", m_nMM);
    manufactureIniRead.setValue("TIME/m_nHH", m_nHH);
    manufactureIniRead.setValue("TIME/m_nDay", m_nDay);




    manufactureIniRead.setValue("CCD1/m_nDefectAbn", m_tAlg1->m_worker->m_nDefectAbn);
    manufactureIniRead.setValue("CCD1/m_nRectAbn", m_tAlg1->m_worker->m_nRectAbn);
    manufactureIniRead.setValue("CCD2/m_nDefectAbn", m_tAlg2->m_worker->m_nDefectAbn);
    manufactureIniRead.setValue("CCD2/m_nRectAbn", m_tAlg2->m_worker->m_nRectAbn);
    manufactureIniRead.setValue("CCD3/m_nDefectAbn", m_tAlg3->m_worker->m_nDefectAbn);
    manufactureIniRead.setValue("CCD3/m_nRectAbn", m_tAlg3->m_worker->m_nRectAbn);
    manufactureIniRead.setValue("CCD4/m_nDefectAbn", m_tAlg4->m_worker->m_nDefectAbn);
    manufactureIniRead.setValue("CCD4/m_nRectAbn", m_tAlg4->m_worker->m_nRectAbn);
    manufactureIniRead.setValue("CCD5/m_nDefectAbn", m_tAlg5->m_worker->m_nDefectAbn);
    manufactureIniRead.setValue("CCD5/m_nRectAbn", m_tAlg5->m_worker->m_nRectAbn);
    manufactureIniRead.setValue("CCD6/m_nDefectAbn", m_tAlg6->m_worker->m_nDefectAbn);
    manufactureIniRead.setValue("CCD6/m_nRectAbn", m_tAlg6->m_worker->m_nRectAbn);
    manufactureIniRead.setValue("CCD7/m_nDefectAbn", m_tAlg7->m_worker->m_nDefectAbn);
    manufactureIniRead.setValue("CCD7/m_nRectAbn", m_tAlg7->m_worker->m_nRectAbn);
    manufactureIniRead.setValue("CCD8/m_nDefectAbn", m_tAlg8->m_worker->m_nDefectAbn);
    manufactureIniRead.setValue("CCD8/m_nRectAbn", m_tAlg8->m_worker->m_nRectAbn);


    // 存储Alg统计数据

    float* fAlgOK;
    float* fAlgNG;
    float* fAlgUp;
    float* fAlgDown;




    /*QList<AlgThread*> m_lAlg = QList<AlgThread*>() << m_tAlg1 << m_tAlg2 << m_tAlg3 << m_tAlg4
                                                   << m_tAlg5 << m_tAlg6 << m_tAlg7 << m_tAlg8;

    for (int i = 0; i < 8; i++){
        fAlgOK = &m_lAlg[i]->m_worker->m_nAlgOK.dM_R_DataA.dWidth;
        fAlgNG = &m_lAlg[i]->m_worker->m_nAlgNG.dM_R_DataA.dWidth;
        fAlgUp = &m_lAlg[i]->m_worker->m_nAlgUp.dM_R_DataA.dWidth;
        fAlgDown = &m_lAlg[i]->m_worker->m_nAlgDown.dM_R_DataA.dWidth;


        for (int j = 0; j < 31; j++){
            manufactureIniRead.setValue(QString("Alg%1_OK/parame%2").arg(i + 1).arg(j), *fAlgOK);
            manufactureIniRead.setValue(QString("Alg%1_NG/parame%2").arg(i + 1).arg(j), *fAlgNG);
            manufactureIniRead.setValue(QString("Alg%1_Up/parame%2").arg(i + 1).arg(j), *fAlgUp);
            manufactureIniRead.setValue(QString("Alg%1_Down/parame%2").arg(i + 1).arg(j), *fAlgDown);


            fAlgOK++;
            fAlgNG++;
            fAlgUp++;
            fAlgDown++;
        }
    }*/
}

void MainWindow::RdManufactureIni()
{
    // 获取上一次程序运行后的数据


    QString manufacturePath = "Config/manufacture.ini";
    QSettings manufactureIniRead(manufacturePath, QSettings::IniFormat);
    manufactureIniRead.setIniCodec(QTextCodec::codecForName("UTF_8"));


    // 获取产品类型
    m_nProductNum = manufactureIniRead.value("ProductNum/Num").toInt();


    m_nCameraResult = manufactureIniRead.value("CM/m_nCameraResult").toInt();
    m_nGod1 = manufactureIniRead.value("CM/m_nGod1").toInt();
    m_nGod2 = manufactureIniRead.value("CM/m_nGod2").toInt();
    m_nGod3 = manufactureIniRead.value("CM/m_nGod3").toInt();
    m_nGod4 = manufactureIniRead.value("CM/m_nGod4").toInt();
    m_nGod5 = manufactureIniRead.value("CM/m_nGod5").toInt();
    m_nGod6 = manufactureIniRead.value("CM/m_nGod6").toInt();
    m_nGod7 = manufactureIniRead.value("CM/m_nGod7").toInt();
    m_nGod8 = manufactureIniRead.value("CM/m_nGod8").toInt();


    m_nBad1 = manufactureIniRead.value("CM/m_nBad1").toInt();
    m_nBad2 = manufactureIniRead.value("CM/m_nBad2").toInt();
    m_nBad3 = manufactureIniRead.value("CM/m_nBad3").toInt();
    m_nBad4 = manufactureIniRead.value("CM/m_nBad4").toInt();
    m_nBad5 = manufactureIniRead.value("CM/m_nBad5").toInt();
    m_nBad6 = manufactureIniRead.value("CM/m_nBad6").toInt();
    m_nBad7 = manufactureIniRead.value("CM/m_nBad7").toInt();
    m_nBad8 = manufactureIniRead.value("CM/m_nBad8").toInt();

    m_nNG1 = manufactureIniRead.value("SUM/m_nNG1").toInt();
    m_nNG2 = manufactureIniRead.value("SUM/m_nNG2").toInt();
    m_nNG3 = manufactureIniRead.value("SUM/m_nNG3").toInt();
    m_nNG4 = manufactureIniRead.value("SUM/m_nNG4").toInt();
    m_nOK = manufactureIniRead.value("SUM/m_nOK").toInt();

    m_nSS = manufactureIniRead.value("TIME/m_nSS").toInt();
    m_nMM = manufactureIniRead.value("TIME/m_nMM").toInt();
    m_nHH = manufactureIniRead.value("TIME/m_nHH").toInt();
    m_nDay = manufactureIniRead.value("TIME/m_nDay").toInt();

    m_tAlg1->m_worker->m_nDefectAbn = manufactureIniRead.value("CCD1/m_nDefectAbn").toInt();
    m_tAlg1->m_worker->m_nRectAbn = manufactureIniRead.value("CCD1/m_nRectAbn").toInt();
    m_tAlg2->m_worker->m_nDefectAbn = manufactureIniRead.value("CCD2/m_nDefectAbn").toInt();
    m_tAlg2->m_worker->m_nRectAbn = manufactureIniRead.value("CCD2/m_nRectAbn").toInt();
    m_tAlg3->m_worker->m_nDefectAbn = manufactureIniRead.value("CCD3/m_nDefectAbn").toInt();
    m_tAlg3->m_worker->m_nRectAbn = manufactureIniRead.value("CCD3/m_nRectAbn").toInt();
    m_tAlg4->m_worker->m_nDefectAbn = manufactureIniRead.value("CCD4/m_nDefectAbn").toInt();
    m_tAlg4->m_worker->m_nRectAbn = manufactureIniRead.value("CCD4/m_nRectAbn").toInt();
    m_tAlg5->m_worker->m_nDefectAbn = manufactureIniRead.value("CCD5/m_nDefectAbn").toInt();
    m_tAlg5->m_worker->m_nRectAbn = manufactureIniRead.value("CCD5/m_nRectAbn").toInt();
    m_tAlg6->m_worker->m_nDefectAbn = manufactureIniRead.value("CCD6/m_nDefectAbn").toInt();
    m_tAlg6->m_worker->m_nRectAbn = manufactureIniRead.value("CCD6/m_nRectAbn").toInt();

    m_tAlg7->m_worker->m_nDefectAbn = manufactureIniRead.value("CCD7/m_nDefectAbn").toInt();
    m_tAlg7->m_worker->m_nRectAbn = manufactureIniRead.value("CCD7/m_nRectAbn").toInt();
    m_tAlg8->m_worker->m_nDefectAbn = manufactureIniRead.value("CCD8/m_nDefectAbn").toInt();
    m_tAlg8->m_worker->m_nRectAbn = manufactureIniRead.value("CCD8/m_nRectAbn").toInt();


    ShowUI();
    SetRunTime();

    // 读取Alg统计数据

    float* fAlgOK;
    float* fAlgNG;
    float* fAlgUp;
    float* fAlgDown;



    QList<AlgThread*> m_lAlg = QList<AlgThread*>() << m_tAlg1 << m_tAlg2 << m_tAlg3 << m_tAlg4
                                                   << m_tAlg5 << m_tAlg6 << m_tAlg7 << m_tAlg8;

    for (int i = 0; i < 8; i++){
        fAlgOK = &m_lAlg[i]->m_worker->m_nAlgOK.dM_R_DataA.dWidth;
        fAlgNG = &m_lAlg[i]->m_worker->m_nAlgNG.dM_R_DataA.dWidth;
        fAlgUp = &m_lAlg[i]->m_worker->m_nAlgUp.dM_R_DataA.dWidth;
        fAlgDown = &m_lAlg[i]->m_worker->m_nAlgDown.dM_R_DataA.dWidth;

        for (int j = 0; j < 31; j++){
            *fAlgOK = manufactureIniRead.value(QString("Alg%1_OK/parame%2").arg(i + 1).arg(j)).toFloat();
            *fAlgNG = manufactureIniRead.value(QString("Alg%1_NG/parame%2").arg(i + 1).arg(j)).toFloat();
            *fAlgUp = manufactureIniRead.value(QString("Alg%1_Up/parame%2").arg(i + 1).arg(j)).toFloat();
            *fAlgDown = manufactureIniRead.value(QString("Alg%1_Down/parame%2").arg(i + 1).arg(j)).toFloat();


            fAlgOK++;
            fAlgNG++;
            fAlgUp++;
            fAlgDown++;
        }
    }
}

void MainWindow::AlgSwResult()
{
    if (m_nMainCurrent){
        m_tCurrentAlg->m_worker->AlgSw1 = m_dDataTab->m_bAlgSw1;
        m_tCurrentAlg->m_worker->AlgSw2 = m_dDataTab->m_bAlgSw2;
    }
}

void MainWindow::on_checkBox_clicked(bool checked)
{
    m_nSaveData = checked;
}

void MainWindow::on_Main_BtOpenFile_clicked()
{
    // 查看数据文件

    QString runPath = QDir::currentPath() + "/data";     //获取当前路径。
    QFile bfilePath(runPath);
    if (!bfilePath.exists()){
        return;
    }
    QString filePath = "file:///" + runPath;
    QDesktopServices::openUrl(QUrl::fromLocalFile(filePath));
}





void MainWindow::on_Main_BtLogManage_clicked()
{
    //用户管理
    m_dLog->serachUserData();
    m_dLog->show();
   
}

